12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using JetBrains.Annotations;
- using LLM.Editor.Helper;
- using UnityEditor;
- using UnityEngine;
- namespace LLM.Editor.Commands
- {
- [System.Serializable]
- public class SetHierarchyParams
- {
- public string childIdentifier;
- public string parentIdentifier; // null or empty indicates root
- public int siblingIndex = -1; // -1 indicates end of list
- }
- [UsedImplicitly]
- public class SetHierarchyCommand : ICommand
- {
- private readonly SetHierarchyParams _params;
- public SetHierarchyCommand(string jsonParams)
- {
- _params = jsonParams?.FromJson<SetHierarchyParams>();
- }
- public CommandOutcome Execute(Data.CommandContext context)
- {
- if (_params == null || string.IsNullOrEmpty(_params.childIdentifier))
- {
- context.ErrorMessage = "Invalid parameters: childIdentifier is required.";
- return CommandOutcome.Error;
- }
- var childObject = CommandUtility.ResolveIdentifier(context, _params.childIdentifier);
- if (childObject is not GameObject childGo)
- {
- context.ErrorMessage = $"Could not find a valid child GameObject with logical name: '{_params.childIdentifier}'";
- return CommandOutcome.Error;
- }
- Transform parentTransform = null;
- if (!string.IsNullOrEmpty(_params.parentIdentifier))
- {
- var parentObject = CommandUtility.ResolveIdentifier(context, _params.parentIdentifier);
- if (parentObject is not GameObject parentGo)
- {
- context.ErrorMessage = $"Could not find a valid parent GameObject with logical name: '{_params.parentIdentifier}'";
- return CommandOutcome.Error;
- }
- parentTransform = parentGo.transform;
- }
-
- Undo.SetTransformParent(childGo.transform, parentTransform, "Reparent GameObject");
- if (_params.siblingIndex > -1)
- {
- childGo.transform.SetSiblingIndex(_params.siblingIndex);
- }
- Debug.Log($"[SetHierarchyCommand] Set parent of '{childGo.name}' to '{(parentTransform ? parentTransform.name : "root")}'.");
- return CommandOutcome.Success;
- }
- }
- }
|