DeleteGameObjectCommand.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using JetBrains.Annotations;
  2. using LLM.Editor.Helper;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace LLM.Editor.Commands
  6. {
  7. [System.Serializable]
  8. public class DeleteGameObjectParams
  9. {
  10. public int targetIdentifier;
  11. }
  12. [UsedImplicitly]
  13. public class DeleteGameObjectCommand : ICommand
  14. {
  15. private readonly DeleteGameObjectParams _params;
  16. public DeleteGameObjectCommand(string jsonParams)
  17. {
  18. _params = jsonParams?.FromJson<DeleteGameObjectParams>();
  19. }
  20. public CommandOutcome Execute(Data.CommandContext context)
  21. {
  22. if (_params == null || _params.targetIdentifier == 0)
  23. {
  24. context.ErrorMessage = "Invalid parameters: targetIdentifier is required.";
  25. Debug.LogError($"[DeleteGameObjectCommand] {context.ErrorMessage}");
  26. return CommandOutcome.Error;
  27. }
  28. var targetObject = EditorUtility.InstanceIDToObject(_params.targetIdentifier);
  29. if (!targetObject)
  30. {
  31. context.ErrorMessage = $"Could not find GameObject with ID: {_params.targetIdentifier}";
  32. Debug.LogError($"[DeleteGameObjectCommand] {context.ErrorMessage}");
  33. return CommandOutcome.Error;
  34. }
  35. if (AssetDatabase.Contains(targetObject))
  36. {
  37. context.ErrorMessage = $"Safety check failed: Attempted to delete an asset, not a scene object. Path: {AssetDatabase.GetAssetPath(targetObject)}";
  38. Debug.LogError($"[DeleteGameObjectCommand] {context.ErrorMessage}");
  39. return CommandOutcome.Error;
  40. }
  41. Object.DestroyImmediate(targetObject);
  42. Debug.Log($"[DeleteGameObjectCommand] Deleted object with previous ID: {_params.targetIdentifier}");
  43. return CommandOutcome.Success;
  44. }
  45. }
  46. }