InstantiatePrefabCommand.cs 1.1 KB

123456789101112131415161718192021222324252627282930
  1. using UnityEditor;
  2. using UnityEngine;
  3. using JetBrains.Annotations;
  4. namespace LLM.Editor.Commands
  5. {
  6. [UsedImplicitly]
  7. public class InstantiatePrefabCommand : ICommand
  8. {
  9. public InstantiatePrefabCommand(string jsonParams) { } // No params needed for this one
  10. public CommandOutcome Execute(Data.CommandContext context)
  11. {
  12. if (context.CurrentSubject is not string prefabPath)
  13. {
  14. Debug.LogError("[InstantiatePrefabCommand] The current subject is not a valid prefab path.");
  15. return CommandOutcome.Error;
  16. }
  17. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  18. var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
  19. Undo.RegisterCreatedObjectUndo(instance, "Instantiate " + instance.name);
  20. Selection.activeObject = instance;
  21. Debug.Log($"[InstantiatePrefabCommand] Instantiated '{prefab.name}' into the scene.");
  22. context.CurrentSubject = instance;
  23. return CommandOutcome.Success;
  24. }
  25. }
  26. }