ProjectSettingsProvider.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace LLM.Editor.Analysis
  8. {
  9. /// <summary>
  10. /// Provides access to Unity's project-wide settings assets by reflecting on their C# types.
  11. /// This returns the true, programmatic property names needed for modification.
  12. /// </summary>
  13. public class ProjectSettingsProvider : IContextProvider
  14. {
  15. public object GetContext(Object target, string qualifier)
  16. {
  17. if (string.IsNullOrEmpty(qualifier))
  18. {
  19. return "Error: A settings asset name (qualifier) is required (e.g., 'QualitySettings', 'TimeManager', 'GraphicsSettings').";
  20. }
  21. var assetPath = $"ProjectSettings/{qualifier}.asset";
  22. var settingsObjects = AssetDatabase.LoadAllAssetsAtPath(assetPath);
  23. if (settingsObjects == null || settingsObjects.Length == 0)
  24. {
  25. return $"Error: Could not find project settings file at '{assetPath}'. Please ensure the qualifier is a valid settings asset name.";
  26. }
  27. // Find the main settings object, which often matches the file name.
  28. var mainSettingsObject = settingsObjects.FirstOrDefault(o => o != null && o.GetType().Name == qualifier)
  29. ?? settingsObjects.FirstOrDefault();
  30. if (mainSettingsObject == null)
  31. {
  32. return $"Error: No valid settings object found in '{assetPath}'.";
  33. }
  34. var settingsType = mainSettingsObject.GetType();
  35. var properties = settingsType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
  36. var settingsData = new Dictionary<string, object>();
  37. foreach (var prop in properties)
  38. {
  39. // Ensure the property can be read and is not an indexer property.
  40. if (!prop.CanRead || prop.GetIndexParameters().Length > 0) continue;
  41. try
  42. {
  43. settingsData[prop.Name] = prop.GetValue(mainSettingsObject);
  44. }
  45. catch
  46. {
  47. // Ignore properties that throw exceptions when read.
  48. }
  49. }
  50. return settingsData;
  51. }
  52. }
  53. }