1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using UnityEngine;
- using UnityEditor;
- using LLM.Editor.Helper;
- using JetBrains.Annotations;
- namespace LLM.Editor.Commands
- {
- [Serializable]
- public class CreateGameObjectParams
- {
- public string gameObjectName;
- public string primitiveType; // Optional: e.g., "Cube", "Sphere", "Capsule"
- }
- [UsedImplicitly]
- public class CreateGameObjectCommand : ICommand
- {
- private readonly CreateGameObjectParams _params;
- public CreateGameObjectCommand(string jsonParams)
- {
- _params = jsonParams?.FromJson<CreateGameObjectParams>();
- }
- public CommandOutcome Execute(Data.CommandContext context)
- {
- if (_params == null || string.IsNullOrEmpty(_params.gameObjectName))
- {
- Debug.LogError("[CreateGameObjectCommand] Invalid parameters. A name for the GameObject is required.");
- return CommandOutcome.Error;
- }
- GameObject newGameObject;
- if (string.IsNullOrEmpty(_params.primitiveType))
- {
- // Create an empty GameObject if no primitive type is specified
- newGameObject = new GameObject(_params.gameObjectName);
- }
- else
- {
- // Create a primitive GameObject if a type is specified
- try
- {
- var type = (PrimitiveType)Enum.Parse(typeof(PrimitiveType), _params.primitiveType, true);
- newGameObject = GameObject.CreatePrimitive(type);
- newGameObject.name = _params.gameObjectName;
- }
- catch (ArgumentException)
- {
- Debug.LogError($"[CreateGameObjectCommand] Invalid primitive type: '{_params.primitiveType}'. Valid types are Cube, Sphere, Capsule, Cylinder, Plane, Quad.");
- return CommandOutcome.Error;
- }
- }
-
- Undo.RegisterCreatedObjectUndo(newGameObject, $"Create {newGameObject.name}");
-
- // Set the new object as the current subject so other commands can modify it
- context.CurrentSubject = newGameObject;
-
- Selection.activeGameObject = newGameObject;
- Debug.Log($"[CreateGameObjectCommand] Successfully created new GameObject named '{newGameObject.name}'.");
- return CommandOutcome.Success;
- }
- }
- }
|