InstantiatePrefabCommand.cs 1.1 KB

123456789101112131415161718192021222324252627282930
  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. // Use the context key defined in CreatePrefabCommand
  13. if (!context.transientData.TryGetValue(CreatePrefabCommand.CONTEXT_KEY_PREFAB_PATH, out var pathObj))
  14. {
  15. Debug.LogError("[InstantiatePrefabCommand] Could not find prefab path in context.");
  16. return;
  17. }
  18. var prefabPath = pathObj as string;
  19. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
  20. var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
  21. Undo.RegisterCreatedObjectUndo(instance, "Instantiate " + instance.name);
  22. Selection.activeObject = instance;
  23. Debug.Log($"[InstantiatePrefabCommand] Instantiated '{prefab.name}' into the scene.");
  24. }
  25. }
  26. }