• Unity Inspector编辑器扩展,枚举显示中文,枚举值自定义显示内容


    记录!Unity Inspector面板编辑器扩展,枚举显示中文,枚举值自定义显示内容,显示部分选项。效果如下:

    枚举类代码:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class EnumTest : MonoBehaviour
    5. {
    6. [EnumAttackLevel("攻击级别")]
    7. public EAttackLevel level;
    8. }
    9. public enum EAttackLevel
    10. {
    11. [Header("0空")]
    12. None,
    13. [Header("1低")]
    14. Low,
    15. [Header("2中")]
    16. Med,
    17. [Header("3高")]
    18. High,
    19. }

    扩展类代码:

    1. using System.Collections.Generic;
    2. using UnityEditor;
    3. using UnityEngine;
    4. public class EnumAttackLevelAttribute : HeaderAttribute
    5. {
    6. public EnumAttackLevelAttribute(string header) : base(header)
    7. {
    8. }
    9. }
    10. [CustomPropertyDrawer(typeof(EnumAttackLevelAttribute))]
    11. public class EnumAttackLevelDrawer : PropertyDrawer
    12. {
    13. private readonly List<string> m_displayNames = new List<string>();
    14. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    15. {
    16. var att = (EnumAttackLevelAttribute)attribute;
    17. var type = property.serializedObject.targetObject.GetType();
    18. var field = type.GetField(property.name);
    19. var enumtype = field.FieldType;
    20. foreach (var enumName in property.enumNames)
    21. {
    22. var enumfield = enumtype.GetField(enumName);
    23. if (enumfield.Name == "None")//不显示None
    24. {
    25. continue;
    26. }
    27. var hds = enumfield.GetCustomAttributes(typeof(HeaderAttribute), false);
    28. m_displayNames.Add(hds.Length <= 0 ? enumName : ((HeaderAttribute)hds[0]).header);//如果加了自定义属性,显示自定义名,否则显示枚举选项名
    29. }
    30. EditorGUI.BeginChangeCheck();
    31. var value = EditorGUI.Popup(position, att.header, property.enumValueIndex, m_displayNames.ToArray());
    32. if (EditorGUI.EndChangeCheck())
    33. {
    34. property.enumValueIndex = value + 1;//因为我们隐藏了一个显示项None,这儿别忘了加1
    35. Debug.LogError("value " + property.enumValueIndex.ToString());
    36. }
    37. }
    38. }

    https://www.cnblogs.com/fengxing999/p/12559887.html

  • 相关阅读:
    30.01 C/S、TCP/IP协议妙趣横生、惟妙惟肖谈
    (GCC)STM32基础详解之全局资源的使用
    Java27岁啦——一次争执引起的Java内卷生涯
    Nacos配置中心
    树莓派4B的测试记录(CPU、FFMPEG)
    数据结构与算法之LeetCode-640. 求解方程(数学模拟)
    设计模式基础
    Linux 命令 —— feh
    计算机毕业设计php+vue基于微信小程序的在线挂号预约小程序
    可选链操作符 ?.的用法
  • 原文地址:https://blog.csdn.net/Ling_SevoL_Y/article/details/134041220