1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace LLM.Editor.Analysis
- {
- /// <summary>
- /// Provides access to Unity's project-wide settings assets by reflecting on their C# types.
- /// This returns the true, programmatic property names needed for modification.
- /// </summary>
- public class ProjectSettingsProvider : IContextProvider
- {
- public object GetContext(Object target, string qualifier)
- {
- if (string.IsNullOrEmpty(qualifier))
- {
- return "Error: A settings asset name (qualifier) is required (e.g., 'QualitySettings', 'TimeManager', 'GraphicsSettings').";
- }
- var assetPath = $"ProjectSettings/{qualifier}.asset";
- var settingsObjects = AssetDatabase.LoadAllAssetsAtPath(assetPath);
- if (settingsObjects == null || settingsObjects.Length == 0)
- {
- return $"Error: Could not find project settings file at '{assetPath}'. Please ensure the qualifier is a valid settings asset name.";
- }
-
- // Find the main settings object, which often matches the file name.
- var mainSettingsObject = settingsObjects.FirstOrDefault(o => o != null && o.GetType().Name == qualifier)
- ?? settingsObjects.FirstOrDefault();
- if (mainSettingsObject == null)
- {
- return $"Error: No valid settings object found in '{assetPath}'.";
- }
- var settingsType = mainSettingsObject.GetType();
- var properties = settingsType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
- var settingsData = new Dictionary<string, object>();
- foreach (var prop in properties)
- {
- // Ensure the property can be read and is not an indexer property.
- if (!prop.CanRead || prop.GetIndexParameters().Length > 0) continue;
-
- try
- {
- settingsData[prop.Name] = prop.GetValue(mainSettingsObject);
- }
- catch
- {
- // Ignore properties that throw exceptions when read.
- }
- }
- return settingsData;
- }
- }
- }
|