1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- #endif
- [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
- public class EnumLabelAttribute : PropertyAttribute
- {
- public string label;
- public EnumLabelAttribute(string label)
- {
- this.label = label;
- }
- }
- #if UNITY_EDITOR
- [CustomPropertyDrawer(typeof(EnumLabelAttribute))]
- public class EnumLabelDrawer : PropertyDrawer
- {
- private Dictionary<string, string> customEnumNames = new Dictionary<string, string>();
- public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
- {
- SetUpCustomEnumNames(property, property.enumNames);
- if (property.propertyType == SerializedPropertyType.Enum)
- {
- EditorGUI.BeginChangeCheck();
- string[] displayedOptions = property.enumNames
- .Where(enumName => customEnumNames.ContainsKey(enumName))
- .Select<string, string>(enumName => customEnumNames[enumName])
- .ToArray();
- int selectedIndex = EditorGUI.Popup(position, enumLabelAttribute.label, property.enumValueIndex, displayedOptions);
- if (EditorGUI.EndChangeCheck())
- {
- property.enumValueIndex = selectedIndex;
- }
- }
- }
- private EnumLabelAttribute enumLabelAttribute
- {
- get
- {
- return (EnumLabelAttribute)attribute;
- }
- }
- public void SetUpCustomEnumNames(SerializedProperty property, string[] enumNames)
- {
- Type type = property.serializedObject.targetObject.GetType();
- foreach (FieldInfo fieldInfo in type.GetFields())
- {
- object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(EnumLabelAttribute), false);
- foreach (EnumLabelAttribute customAttribute in customAttributes)
- {
- Type enumType = fieldInfo.FieldType;
- foreach (string enumName in enumNames)
- {
- FieldInfo field = enumType.GetField(enumName);
- if (field == null) continue;
- EnumLabelAttribute[] attrs = (EnumLabelAttribute[])field.GetCustomAttributes(customAttribute.GetType(), false);
- if (!customEnumNames.ContainsKey(enumName))
- {
- foreach (EnumLabelAttribute labelAttribute in attrs)
- {
- customEnumNames.Add(enumName, labelAttribute.label);
- }
- }
- }
- }
- }
- }
- }
- #endif
|