InstantiatePrefabCommand.cs 1.0 KB

1234567891011121314151617181920212223242526272829
  1. using UnityEngine;
  2. using UnityEditor;
  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 void 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;
  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. }
  24. }
  25. }