实现如上图效果
首先我们先新建一个脚本 EnumTest.cs 绑定在一个GameObject上面,里面写一个Enum:
public enum TestEnum
{
test1,
test2,
test3
}
这个时候在unity的 Inspector面板里显示是这样的:
随后我们再新建一个新的脚本:MyAttribute.cs
让MyAttribute继承 PropertyAttribute类型,类中不需要有任何的方法和字段属性
再次新建一个新的脚本:MyPropertyDrawer.cs
先引入 UnityEditor的命名空间:
using UnityEditor;
让MyPropertyDrawer继承PropertyDrawer类型
然后我们重写其中的OnGUI方法:
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
property.intValue = EditorGUI.MaskField(position, label, property.intValue, property.enumNames);
}
在MyPropertyDrawer类的头上加上一个标签:
[[CustomPropertyDrawer(typeof(MyAttribute))]]
回到EnumTest类型,给testEnum字段加上一个标签:
[MyAttribute]
然后返回unity中查看一下: