File was renamed from Utility/EnumLabelAttribute.cs |
| | |
| | | using System;
|
| | | using System; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | |
| | | #endif |
| | | |
| | | [AttributeUsage(AttributeTargets.Field)] |
| | | public class EnumLabelAttribute : PropertyAttribute
|
| | | public class EnumLabelAttribute : PropertyAttribute |
| | | { |
| | | public readonly Type enumType; |
| | | public EnumLabelAttribute(Type type)
|
| | | {
|
| | | this.enumType = type;
|
| | | public EnumLabelAttribute(Type type) |
| | | { |
| | | this.enumType = type; |
| | | } |
| | | } |
| | | |
| | | #if UNITY_EDITOR |
| | | [CustomPropertyDrawer(typeof(EnumLabelAttribute))] |
| | | public class EnumLabelPropertyDrawer : PropertyDrawer
|
| | | {
|
| | | List<string> m_EnumLabels = new List<string>();
|
| | | 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;
|
| | | }
|
| | | }
|
| | | public class EnumLabelPropertyDrawer : PropertyDrawer |
| | | { |
| | | List<string> m_EnumLabels = new List<string>(); |
| | | 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 |