ComponentDataProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using Object = UnityEngine.Object;
  6. namespace LLM.Editor.Analysis
  7. {
  8. /// <summary>
  9. /// Provides detailed, serialized data for a specific component on a GameObject.
  10. /// </summary>
  11. public class ComponentDataProvider : IContextProvider
  12. {
  13. public object GetContext(Object target, string qualifier)
  14. {
  15. if (target is not GameObject go)
  16. {
  17. return "Error: Target must be a GameObject to get component data.";
  18. }
  19. if (string.IsNullOrEmpty(qualifier))
  20. {
  21. return "Error: Component type name (qualifier) is required.";
  22. }
  23. var componentType = GetTypeByName(qualifier);
  24. if (componentType == null)
  25. {
  26. return $"Error: Component type '{qualifier}' not found.";
  27. }
  28. var component = go.GetComponent(componentType);
  29. return !component ? $"Error: Component '{qualifier}' not found on GameObject '{go.name}'." :
  30. // Reuse the generic serialization logic for any component.
  31. SerializeObject(component);
  32. }
  33. private static object SerializeObject(Object unityObject)
  34. {
  35. var serializedObject = new SerializedObject(unityObject);
  36. var propertyIterator = serializedObject.GetIterator();
  37. var propertyData = new Dictionary<string, object>();
  38. if (propertyIterator.NextVisible(true)) // Enter the object
  39. {
  40. while (propertyIterator.NextVisible(false)) // Iterate through properties
  41. {
  42. propertyData[propertyIterator.name] = GetValueFromProperty(propertyIterator);
  43. }
  44. }
  45. return propertyData;
  46. }
  47. private static object GetValueFromProperty(SerializedProperty property)
  48. {
  49. // This is a simplified version. A full implementation would handle all property types.
  50. return property.propertyType switch
  51. {
  52. SerializedPropertyType.Integer => property.longValue,
  53. SerializedPropertyType.Boolean => property.boolValue,
  54. SerializedPropertyType.Float => property.doubleValue,
  55. SerializedPropertyType.String => property.stringValue,
  56. SerializedPropertyType.Color => property.colorValue,
  57. SerializedPropertyType.ObjectReference => GetObjectReferenceValue(property),
  58. SerializedPropertyType.Enum => property.enumNames[property.enumValueIndex],
  59. SerializedPropertyType.Vector2 => property.vector2Value,
  60. SerializedPropertyType.Vector3 => property.vector3Value,
  61. SerializedPropertyType.Vector4 => property.vector4Value,
  62. SerializedPropertyType.Rect => property.rectValue,
  63. SerializedPropertyType.AnimationCurve => property.animationCurveValue,
  64. SerializedPropertyType.Bounds => property.boundsValue,
  65. SerializedPropertyType.Quaternion => property.quaternionValue,
  66. _ => $"Unsupported Type: {property.propertyType}"
  67. };
  68. }
  69. private static object GetObjectReferenceValue(SerializedProperty property)
  70. {
  71. var obj = property.objectReferenceValue;
  72. if (obj == null) return null;
  73. return new { name = obj.name, id = GetStableIdForObject(obj) };
  74. }
  75. private static string GetStableIdForObject(Object obj)
  76. {
  77. if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(obj, out var guid, out long _))
  78. {
  79. if (!string.IsNullOrEmpty(guid) && guid != "00000000000000000000000000000000")
  80. {
  81. return guid;
  82. }
  83. }
  84. return obj.GetInstanceID().ToString();
  85. }
  86. private static Type GetTypeByName(string typeName)
  87. {
  88. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  89. {
  90. var type = assembly.GetType(typeName);
  91. if (type != null) return type;
  92. }
  93. return null;
  94. }
  95. }
  96. }