/// <summary> /// Searches through a class for InspectorField tags creates properties that can be serialized and /// automatically rendered in a custom inspector /// </summary> /// <param name="source"></param> /// <returns></returns> public static List <InspectorPropertySetting> GetSettings(T source) { Type myType = source.GetType(); List <InspectorPropertySetting> settings = new List <InspectorPropertySetting>(); PropertyInfo[] propInfoList = myType.GetProperties(); for (int i = 0; i < propInfoList.Length; i++) { PropertyInfo propInfo = propInfoList[i]; var attrs = (InspectorField[])propInfo.GetCustomAttributes(typeof(InspectorField), false); foreach (var attr in attrs) { settings.Add(InspectorField.FieldToProperty(attr, propInfo.GetValue(source, null), propInfo.Name)); } } FieldInfo[] fieldInfoList = myType.GetFields(); for (int i = 0; i < fieldInfoList.Length; i++) { FieldInfo fieldInfo = fieldInfoList[i]; var attrs = (InspectorField[])fieldInfo.GetCustomAttributes(typeof(InspectorField), false); foreach (var attr in attrs) { settings.Add(InspectorField.FieldToProperty(attr, fieldInfo.GetValue(source), fieldInfo.Name)); } } return(settings); }
/// <summary> /// Copies values from Inspector PropertySettings to an instantiated class on start, /// helps overcome polymorphism limitations of serialization /// </summary> /// <param name="target"></param> /// <param name="settings"></param> public static void LoadSettings(T target, List <InspectorPropertySetting> settings) { Type myType = target.GetType(); PropertyInfo[] propInfoList = myType.GetProperties(); for (int i = 0; i < propInfoList.Length; i++) { PropertyInfo propInfo = propInfoList[i]; var attrs = (InspectorField[])propInfo.GetCustomAttributes(typeof(InspectorField), false); foreach (var attr in attrs) { object value = InspectorField.GetSettingValue(settings, propInfo.Name); propInfo.SetValue(target, value); } } FieldInfo[] fieldInfoList = myType.GetFields(); for (int i = 0; i < fieldInfoList.Length; i++) { FieldInfo fieldInfo = fieldInfoList[i]; var attrs = (InspectorField[])fieldInfo.GetCustomAttributes(typeof(InspectorField), false); foreach (var attr in attrs) { object value = InspectorField.GetSettingValue(settings, fieldInfo.Name); fieldInfo.SetValue(target, value); } } }
public static InspectorPropertySetting FieldToProperty(InspectorField attributes, object fieldValue, string fieldName) { InspectorPropertySetting setting = new InspectorPropertySetting(); setting.Type = attributes.Type; setting.Tooltip = attributes.Tooltip; setting.Label = attributes.Label; setting.Options = attributes.Options; setting.Name = fieldName; setting = UpdatePropertySetting(setting, fieldValue); return(setting); }