虽然mono是支持unicode的。可以在枚举里写中文,但是我还是觉得写英文好一些。可是在编辑器上策划是希望看到的是中文的,还有就是枚举的展示排序功能,策划在编辑的时候为了方便希望把常用的枚举排上前面。

把如下代码放到你的工程里就可以直接用了。

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

using UnityEngine;
using System;
#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 int[] order = new int[0] ;
    public EnumLabelAttribute(string label)
    {
        this.label = label;
    }
    public EnumLabelAttribute(string label,params int[] order)
    {
        this.label = label;
        this.order = order;
    }
}
#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[] indexArray = GetIndexArray (enumLabelAttribute.order);
            if(indexArray.Length != displayedOptions.Length)
            {
                indexArray = new int[displayedOptions.Length];
                for(int i =0; i< indexArray.Length; i++){
                    indexArray[i] = i;
                }
            }
            string[] items = new string[displayedOptions.Length];
            items[0] = displayedOptions[0];      
            for (int i=0; i<displayedOptions.Length; i++) {
                items[i] =  displayedOptions[indexArray[i]];
            }
            int index = -1;
            for (int i=0; i<indexArray.Length; i++) {
                if (indexArray[i] == property.enumValueIndex) {
                    index = i;
                    break;
                }
            }
            if ( (index == -1) && (property.enumValueIndex != -1) ) { SortingError (position,property,label); return; }
            index = EditorGUI.Popup(position, enumLabelAttribute.label,index, items);
            if (EditorGUI.EndChangeCheck())
            {
                if (index >= 0)
                    property.enumValueIndex = indexArray[index];
            }
        }
    }
    private EnumLabelAttribute enumLabelAttribute
    {
        get
        {
            return (EnumLabelAttribute)attribute;
        }
    }
    public void SetUpCustomEnumNames(SerializedProperty property, string[] enumNames)
    {
            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);
                        }
                    }
                }
            }
    }
    int[] GetIndexArray (int[] order)
    {
        int[] indexArray = new int[order.Length];
        for (int i = 0; i < order.Length; i++) {
            int index = 0;
            for (int j = 0; j < order.Length; j++) {                
                if (order[i] > order[j]) {                  
                    index++;                
                }              
            }
            indexArray[i] = index;
        }
        return (indexArray);
    }
    void SortingError (Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.PropertyField(position, property, new GUIContent(label.text + " (sorting error)"));
        EditorGUI.EndProperty();
    }
}
public class EnumLabel
{
    static public object GetEnum(Type type, SerializedObject serializedObject, string path)
    {
        SerializedProperty property =  GetPropety(serializedObject,path);
        return  System.Enum.GetValues(type).GetValue(property.enumValueIndex);
    }
    static public object DrawEnum(Type type, SerializedObject serializedObject, string path)
    {
        return DrawEnum(type,serializedObject, GetPropety(serializedObject,path));
    }
    static public object DrawEnum(Type type, SerializedObject serializedObject,SerializedProperty property)
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(property);
        serializedObject.ApplyModifiedProperties();
        return  System.Enum.GetValues(type).GetValue(property.enumValueIndex);
    }
    static public SerializedProperty GetPropety(SerializedObject serializedObject, string path)
    {
        string []contents = path.Split('/');
        SerializedProperty property =  serializedObject.FindProperty(contents[0]);
        for(int i=1; i< contents.Length; i++){
            property = property.FindPropertyRelative(contents[i]);
        }
        return property;
    }
}
#endif

使用是这样的,第二个参数就是排序。接收int的不固定参数。

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
    [EnumLabel("我是的类型",10,1,5,2)]
    public NewType newType = NewType.One;
}
public enum NewType : byte
{
    [EnumLabel("我是1")]
    One = 10,
    [EnumLabel("我是2")]
    Two = 1,
    [EnumLabel("我是3")]
    Three = 5,
    [EnumLabel("我是4")]
    Four = 2
}

OK 中文与排序都OK了。

但是,有时候我们做编辑器的时候是自己调用OnInspectorGUI来绘制面板的。而且我的枚举对象可能会在另外一个子对象里,或者在子对象里的一个List<T>里面的子对象里。

比如这样, class.data.newType 就是这个类对象的结构,你可以按照你自己类的结构去拼这个字符串。

C#
1
2
3
4
5
6
7
8
9
10

        public override void OnInspectorGU()
        {
            NewType oldType = (NewType)EnumLabel.GetEnum(typeof(NewType),serializedObject,"class/data/newType");
            NewType newType = (NewType)EnumLabel.DrawEnum(typeof(NewType),serializedObject,"class/data/newType");
            if(oldType != newType)
            {
                //类型发生改变
            }
        }

还有一种特殊的就是可能枚举在list<T>里,这样在绘制的时候是需要遍历的。

C#
1
2
3
4
5
6
7
8
9
10

       public override void OnInspectorGU()
        {
            SerializedProperty property = EnumLabel.GetPropety(serializedObject,"class/datas");
            for(int i =0; i<  count; i++)
            {
                SerializedProperty eProperty = property.GetArrayElementAtIndex(i);
                NewType newType = (NewType)EnumLabel.DrawEnum(typeof(NewType),serializedObject
                            ,eProperty.FindPropertyRelative("newType"));
            }
        }

OK大功告成。

参考文章:

https://github.com/anchan828/property-drawer-collection/blob/master/EnumLabel/EnumLabelAttribute.cs

http://forum.unity3d.com/threads/enum-inspector-sorting-attribute.357558/

  • 本文固定链接: http://www.xuanyusong.com/archives/4213
  • 转载请注明: 雨松MOMO 2016年07月13日 于 雨松MOMO程序研究院 发表

Unity3D研究院之Inspector面板枚举的别名与排序相关推荐

  1. Unity3D研究院之鼠标控制角色移动与奔跑示例

    最新补充.          一般在做鼠标选择时是从摄像机向目标点发送一条射线,然后取得射线与对象相交的点来计算3D目标点.后来在开发中发现了一个问题(射线被别的对象挡住了),就是如果主角的前面有别的 ...

  2. Unity编辑器教程用法——自定义Inspector 面板编辑器Chinar

    Chinar blog :www.chinar.xin 自定义Inspector 面板编辑器 本文提供全流程,中文翻译. Chinar 的初衷是将一种简单的生活方式带给世人 使有限时间 具备无限可能 ...

  3. Unity3D研究院之与Android相互传递消息

       上一篇文章我们学习了Unity向Android发送消息,如果Android又能给Unity回馈消息那么这就玩美了.恰好Unity for Andoid 和 IOS一样都是可以相互与Unity发送 ...

  4. 转:Unity3D研究院之提取游戏资源的三个工具支持Unity5(八十四)

    这两天无意间又发现了两个提取Unity游戏资源的工具,这会儿刚好有时间我就码点字总结一下. 一.disunity 因为之前写过了所以这里就不介绍了 .Unity3D研究院之mac上从.ipa中提取un ...

  5. unity Inspector 面板扩展

    通常情况下,我们定义了一个脚本1,公开了一些变量 脚本1: using System.Collections; using System.Collections.Generic; using Unit ...

  6. Unity3D研究院之挥动武器产生的剑痕特效(四十七)

    雨松MOMO 最近超级忙,好久没写东西啦.前几天学习了一下如何实现剑痕特效.用公司的模型写例子不太好,所以我还是用以前那个小牛头人 嘿嘿.如下图所示,你懂得蛤蛤. 目前已知3种方法可以做这种剑痕特效 ...

  7. Unity3D研究院之Unity中连接本地或局域网MySQL数据库

    用户名 Email 游戏蛮牛 手机端 开启辅助访问 腾讯QQ 立即注册 登录 用户名 自动登录  找回密码 密码 登录  注册帐号 [Unity5.X版本开始预售啦!] 扫一扫,访问微社区 </ ...

  8. Unity3D研究院之在Unity中打开第三方数据库配合Android开发(三十二)

    http://www.xuanyusong.com/archives/831 http://www.xuanyusong.com/archives/1454 如果大家对Unity中如何使用数据库还不是 ...

  9. 【Unity】讲解如何在Unity的Inspector面板中用滑动条来控制变量的大小

    首先,我们现在的需求是这样的,我定义了一个脚本,里面有一个int类型的变量,但是我想控制变量的大小在0到100之间,通过用滑动条的方式来控制. 其实这里的player HP 是我使用了unity自带的 ...

最新文章

  1. Win7封装无损廋身清单
  2. 这个医疗AI准确率突破天际,招来了铺天盖地的质疑
  3. MySQL中如何关闭事务的自动提交
  4. LeetCode MySQL 578. 查询回答率最高的问题
  5. 电商千万级交易的金手指:分布式事务管理
  6. git 学习1--查看全局配置
  7. 查看php文件的效果,HTML5的交互式动画效果文件夹预览查看特效
  8. python3.7如何使用enum_python3 enum模块
  9. RHEL 6.3的yum不小心被删除了。如何恢复?
  10. Java编程练习题3
  11. spring boot 设置启动时初始化DispatcherServlet
  12. 航空运输行业:优质民营航司的黄金期才刚刚开始-20210106.PDF
  13. Window系统怎么如何激活?详细版
  14. echarts-半圆
  15. combo 技术简单介绍
  16. vscode 关闭 编辑框右侧的 预览框
  17. 海淀区第九届单片机竞赛获奖名单_2014年北京市中小学生单片机获奖名单-获奖名单...
  18. CSS超基础,快速入门
  19. gmssl 国密ssl流程测试
  20. 本地docker不能登录远程harbor服务器,error response from daemon,error parsing http 403 response body

热门文章

  1. 部署并使用Docker(Alibaba Cloud Linux 2)
  2. java最小访问原则_Android基础进阶之EffectiveJava翻译系列(第七章:通用原则)
  3. halcon的算子清点: Chapter 1 分类
  4. 深入理解ROS技术 【3】ROS下的模块详解(129-180)
  5. matlab潮流程序,IEEE33节点matlab潮流程序.doc
  6. 应用程序池超出其作业限制设置_网站改版注意事项 - 蜘蛛池
  7. Python基于聚类算法实现密度聚类(DBSCAN)计算
  8. c语言双分支结构运算符,c语言——运算符、分支结构、循环结构
  9. 批量模糊查询_Django之ORM表高级操作、增删改查、F/Q查询等
  10. MyBatis使用ResultMap处理一对多多对一