AddComponentToAssetCommand.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using LLM.Editor.Helper;
  6. using JetBrains.Annotations;
  7. using Object = UnityEngine.Object;
  8. namespace LLM.Editor.Commands
  9. {
  10. [Serializable]
  11. public class AddComponentParams
  12. {
  13. public string scriptName;
  14. public string componentName;
  15. public string targetIdentifier; // This is now a LOGICAL name
  16. public string childPath; // Optional path to a child object, e.g., "Launcher/SpawnPoint"
  17. }
  18. [UsedImplicitly]
  19. public class AddComponentToAssetCommand : ICommand
  20. {
  21. private readonly AddComponentParams _params;
  22. public AddComponentToAssetCommand(string jsonParams)
  23. {
  24. _params = jsonParams?.FromJson<AddComponentParams>();
  25. }
  26. public CommandOutcome Execute(Data.CommandContext context)
  27. {
  28. if (_params == null || string.IsNullOrEmpty(_params.targetIdentifier) || (string.IsNullOrEmpty(_params.scriptName) && string.IsNullOrEmpty(_params.componentName)))
  29. {
  30. context.ErrorMessage = "Invalid parameters. Target identifier and script/component name are required.";
  31. return CommandOutcome.Error;
  32. }
  33. _params.scriptName ??= _params.componentName;
  34. var targetObject = CommandUtility.ResolveIdentifier(context, _params.targetIdentifier);
  35. if (!targetObject)
  36. {
  37. context.ErrorMessage = $"Could not find target with logical name '{_params.targetIdentifier}'. The object may have been deleted or the identifier map is out of sync.";
  38. return CommandOutcome.Error;
  39. }
  40. var scriptType = GetTypeByName(_params.scriptName);
  41. if (scriptType == null)
  42. {
  43. context.ErrorMessage = $"Could not find script type '{_params.scriptName}'. It may not have compiled correctly or does not exist.";
  44. return CommandOutcome.Error;
  45. }
  46. if (targetObject is not GameObject rootGo)
  47. {
  48. context.ErrorMessage = $"Target '{targetObject.name}' is not a GameObject and components cannot be added to it.";
  49. return CommandOutcome.Error;
  50. }
  51. var objectToAddComponentTo = rootGo;
  52. if (!string.IsNullOrEmpty(_params.childPath))
  53. {
  54. var childTransform = rootGo.transform.Find(_params.childPath);
  55. if (childTransform)
  56. {
  57. objectToAddComponentTo = childTransform.gameObject;
  58. }
  59. else
  60. {
  61. context.ErrorMessage = $"Could not find child at path '{_params.childPath}' on target '{rootGo.name}'.";
  62. return CommandOutcome.Error;
  63. }
  64. }
  65. // Handle whether we're editing a prefab asset or a scene instance
  66. if (PrefabUtility.IsPartOfPrefabAsset(objectToAddComponentTo))
  67. {
  68. var prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(targetObject);
  69. var prefabContents = PrefabUtility.LoadPrefabContents(prefabPath);
  70. var targetInPrefab = FindEquivalentObjectInPrefab(objectToAddComponentTo, prefabContents);
  71. if (targetInPrefab)
  72. {
  73. targetInPrefab.AddComponent(scriptType);
  74. PrefabUtility.SaveAsPrefabAsset(prefabContents, prefabPath);
  75. PrefabUtility.UnloadPrefabContents(prefabContents);
  76. Debug.Log($"[AddComponentCommand] Added component '{_params.scriptName}' to prefab at '{prefabPath}'.");
  77. }
  78. else
  79. {
  80. PrefabUtility.UnloadPrefabContents(prefabContents);
  81. context.ErrorMessage = $"Could not find equivalent of '{objectToAddComponentTo.name}' in prefab contents.";
  82. return CommandOutcome.Error;
  83. }
  84. }
  85. else // It's a scene object
  86. {
  87. Undo.AddComponent(objectToAddComponentTo, scriptType);
  88. Debug.Log($"[AddComponentCommand] Added component '{_params.scriptName}' to scene object '{objectToAddComponentTo.name}'.");
  89. }
  90. return CommandOutcome.Success;
  91. }
  92. private static GameObject FindEquivalentObjectInPrefab(GameObject original, GameObject prefabRoot)
  93. {
  94. if (original.name == prefabRoot.name) return prefabRoot;
  95. var originalPath = AnimationUtility.CalculateTransformPath(original.transform, original.transform.root);
  96. var foundChild = prefabRoot.transform.Find(originalPath);
  97. return foundChild ? foundChild.gameObject : null;
  98. }
  99. private static Type GetTypeByName(string typeName)
  100. {
  101. return AppDomain.CurrentDomain.GetAssemblies()
  102. .Select(assembly => assembly.GetType(typeName, false)) // Be non-throwing
  103. .FirstOrDefault(type => type != null);
  104. }
  105. }
  106. }