1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using UnityEditor;
- using UnityEngine;
- namespace LLM.Editor.Commands
- {
- public static class CommandUtility
- {
- public static Object ResolveIdentifier(Data.CommandContext context, string logicalName)
- {
- if (string.IsNullOrEmpty(logicalName))
- {
- context.ErrorMessage = "Identifier is null or empty.";
- return null;
- }
- if (!context.IdentifierMap.TryGetValue(logicalName, out var persistentId))
- {
- // Fallback for objects that might have been selected by the user directly
- // and therefore won't be in the map.
- var foundObject = GameObject.Find(logicalName);
- if (foundObject) return foundObject;
-
- context.ErrorMessage = $"Logical name '{logicalName}' not found in the identifier map.";
- return null;
- }
- // Check if it's a scene object path
- if (persistentId.StartsWith("/"))
- {
- var go = GameObject.Find(persistentId);
- if (!go)
- {
- context.ErrorMessage = $"Could not find GameObject at path: '{persistentId}' for logical name '{logicalName}'. It may have been deleted or renamed.";
- }
- return go;
- }
- if (int.TryParse(persistentId, out var sceneObjectId))
- {
- var go = EditorUtility.InstanceIDToObject(sceneObjectId);
- if (go == null)
- {
- context.ErrorMessage = $"Could not find GameObject with InstanceID: '{sceneObjectId}' for logical name '{logicalName}'. It may have been deleted or renamed.";
- }
- return go;
- }
-
- // Assume it's a GUID for a project asset
- var assetPath = AssetDatabase.GUIDToAssetPath(persistentId);
- if (string.IsNullOrEmpty(assetPath))
- {
- context.ErrorMessage = $"Could not find asset path for GUID: '{persistentId}' for logical name '{logicalName}'.";
- return null;
- }
-
- return AssetDatabase.LoadAssetAtPath<Object>(assetPath);
- }
- }
- }
|