using System; using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; using System.Reflection; #endif [AttributeUsage(AttributeTargets.Field)] public class EnumLabelAttribute : PropertyAttribute { public readonly Type enumType; public EnumLabelAttribute(Type type) { this.enumType = type; } } #if UNITY_EDITOR [CustomPropertyDrawer(typeof(EnumLabelAttribute))] public class EnumLabelPropertyDrawer : PropertyDrawer { List m_EnumLabels = new List(); public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var customAttribute = (EnumLabelAttribute)attribute; if (m_EnumLabels.Count != property.enumNames.Length) { m_EnumLabels.Clear(); var enumtype = customAttribute.enumType; foreach (var enumName in property.enumNames) { var enumfield = enumtype.GetField(enumName); var customAttributes = enumfield.GetCustomAttributes(typeof(HeaderAttribute), false); m_EnumLabels.Add(customAttributes.Length <= 0 ? enumName : ((HeaderAttribute)customAttributes[0]).header); } } EditorGUI.BeginChangeCheck(); var value = EditorGUI.Popup(position, fieldInfo.Name, property.enumValueIndex, m_EnumLabels.ToArray()); if (EditorGUI.EndChangeCheck()) { property.enumValueIndex = value; } } } #endif