CommandUtility.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace LLM.Editor.Commands
  4. {
  5. public static class CommandUtility
  6. {
  7. public static Object ResolveIdentifier(Data.CommandContext context, string logicalName)
  8. {
  9. if (string.IsNullOrEmpty(logicalName))
  10. {
  11. context.ErrorMessage = "Identifier is null or empty.";
  12. return null;
  13. }
  14. if (!context.IdentifierMap.TryGetValue(logicalName, out var persistentId))
  15. {
  16. // Fallback for objects that might have been selected by the user directly
  17. // and therefore won't be in the map.
  18. var foundObject = GameObject.Find(logicalName);
  19. if (foundObject) return foundObject;
  20. context.ErrorMessage = $"Logical name '{logicalName}' not found in the identifier map.";
  21. return null;
  22. }
  23. // Check if it's a scene object path
  24. if (persistentId.StartsWith("/"))
  25. {
  26. var go = GameObject.Find(persistentId);
  27. if (!go)
  28. {
  29. context.ErrorMessage = $"Could not find GameObject at path: '{persistentId}' for logical name '{logicalName}'. It may have been deleted or renamed.";
  30. }
  31. return go;
  32. }
  33. if (int.TryParse(persistentId, out var sceneObjectId))
  34. {
  35. var go = EditorUtility.InstanceIDToObject(sceneObjectId);
  36. if (go == null)
  37. {
  38. context.ErrorMessage = $"Could not find GameObject with InstanceID: '{sceneObjectId}' for logical name '{logicalName}'. It may have been deleted or renamed.";
  39. }
  40. return go;
  41. }
  42. // Assume it's a GUID for a project asset
  43. var assetPath = AssetDatabase.GUIDToAssetPath(persistentId);
  44. if (string.IsNullOrEmpty(assetPath))
  45. {
  46. context.ErrorMessage = $"Could not find asset path for GUID: '{persistentId}' for logical name '{logicalName}'.";
  47. return null;
  48. }
  49. return AssetDatabase.LoadAssetAtPath<Object>(assetPath);
  50. }
  51. }
  52. }