SelectionProvider.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using UnityEditor;
  2. using System.Collections.Generic;
  3. using Object = UnityEngine.Object;
  4. namespace LLM.Editor.Analysis
  5. {
  6. /// <summary>
  7. /// Provides context about the currently selected objects in the Unity Editor.
  8. /// Distinguishes between objects in the scene and assets in the project.
  9. /// </summary>
  10. public class SelectionProvider : IContextProvider
  11. {
  12. public class SelectionResult
  13. {
  14. public string name;
  15. public string id;
  16. public string type;
  17. public bool isSceneObject;
  18. public string assetPath;
  19. }
  20. public object GetContext(Object target, string qualifier)
  21. {
  22. var selectedObjects = Selection.objects;
  23. if (selectedObjects == null || selectedObjects.Length == 0)
  24. {
  25. return "No objects are currently selected in the editor.";
  26. }
  27. var results = new List<SelectionResult>();
  28. foreach (var obj in selectedObjects)
  29. {
  30. var result = new SelectionResult
  31. {
  32. name = obj.name,
  33. type = obj.GetType().FullName
  34. };
  35. if (AssetDatabase.Contains(obj))
  36. {
  37. // It's an asset in the Project window
  38. result.isSceneObject = false;
  39. var path = AssetDatabase.GetAssetPath(obj);
  40. result.assetPath = path;
  41. result.id = AssetDatabase.AssetPathToGUID(path);
  42. }
  43. else
  44. {
  45. // It's a GameObject or component in the Scene
  46. result.isSceneObject = true;
  47. result.assetPath = null;
  48. result.id = obj.GetInstanceID().ToString();
  49. }
  50. results.Add(result);
  51. }
  52. return results;
  53. }
  54. }
  55. }