using UnityEditor; using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.Reflection; /// /// Exposes a property in the inspector /// public static class ExposeProperties { public static void Expose(PropertyField[] properties) { GUILayoutOption[] emptyOptions = new GUILayoutOption[0]; EditorGUILayout.BeginVertical(emptyOptions); foreach (PropertyField field in properties) { EditorGUILayout.BeginHorizontal(emptyOptions); switch (field.Type) { case SerializedPropertyType.Integer: field.SetValue(EditorGUILayout.IntField(field.Name, (int)field.GetValue(), emptyOptions)); break; case SerializedPropertyType.Float: field.SetValue(EditorGUILayout.FloatField(field.Name, (float)field.GetValue(), emptyOptions)); break; case SerializedPropertyType.Boolean: field.SetValue(EditorGUILayout.Toggle(field.Name, (bool)field.GetValue(), emptyOptions)); break; case SerializedPropertyType.String: field.SetValue(EditorGUILayout.TextField(field.Name, (String)field.GetValue(), emptyOptions)); break; case SerializedPropertyType.Vector2: field.SetValue(EditorGUILayout.Vector2Field(field.Name, (Vector2)field.GetValue(), emptyOptions)); break; case SerializedPropertyType.Vector3: field.SetValue(EditorGUILayout.Vector3Field(field.Name, (Vector3)field.GetValue(), emptyOptions)); break; case SerializedPropertyType.Enum: field.SetValue(EditorGUILayout.EnumPopup(field.Name, (Enum)field.GetValue(), emptyOptions)); break; case SerializedPropertyType.ObjectReference: field.SetValue(EditorGUILayout.ObjectField(field.Name, (UnityEngine.Object)field.GetValue(), field.GetPropertyType(), true, emptyOptions)); break; default: break; } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); } public static PropertyField[] GetProperties(object obj) { List fields = new List(); PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo info in infos) { if (!(info.CanRead && info.CanWrite)) continue; object[] attributes = info.GetCustomAttributes(true); bool isExposed = false; foreach (object o in attributes) { if (o.GetType() == typeof(ExposePropertyAttribute)) { isExposed = true; break; } } if (!isExposed) continue; SerializedPropertyType type = SerializedPropertyType.Integer; if (PropertyField.GetPropertyType(info, out type)) { PropertyField field = new PropertyField(obj, info, type); fields.Add(field); } } return fields.ToArray(); } } public class PropertyField { object m_Instance; PropertyInfo m_Info; SerializedPropertyType m_Type; MethodInfo m_Getter; MethodInfo m_Setter; public SerializedPropertyType Type { get { return m_Type; } } public String Name { get { return ObjectNames.NicifyVariableName(m_Info.Name); } } public PropertyField(object instance, PropertyInfo info, SerializedPropertyType type) { m_Instance = instance; m_Info = info; m_Type = type; m_Getter = m_Info.GetGetMethod(); m_Setter = m_Info.GetSetMethod(); } public object GetValue() { return m_Getter.Invoke(m_Instance, null); } public void SetValue(object value) { m_Setter.Invoke(m_Instance, new[] { value }); } public Type GetPropertyType() { return m_Info.PropertyType; } public static bool GetPropertyType(PropertyInfo info, out SerializedPropertyType propertyType) { propertyType = SerializedPropertyType.Generic; Type type = info.PropertyType; if (type == typeof(int)) { propertyType = SerializedPropertyType.Integer; return true; } if (type == typeof(float)) { propertyType = SerializedPropertyType.Float; return true; } if (type == typeof(bool)) { propertyType = SerializedPropertyType.Boolean; return true; } if (type == typeof(string)) { propertyType = SerializedPropertyType.String; return true; } if (type == typeof(Vector2)) { propertyType = SerializedPropertyType.Vector2; return true; } if (type == typeof(Vector3)) { propertyType = SerializedPropertyType.Vector3; return true; } if (type.IsEnum) { propertyType = SerializedPropertyType.Enum; return true; } // COMMENT OUT to NOT expose custom objects/types propertyType = SerializedPropertyType.ObjectReference; return true; } }