2019-01-10Unity编辑器开发,使用CustomPropertyDrawer实现枚举中文显示

标注:https://www.cnblogs.com/CodeGize/p/6892299.html


 在Unity开发中,枚举常常被用到。但是Unity自身对于枚举值,并不能做好中文的支持。无论是Head或者ToolTip.如下例:

using UnityEngine;publicclass EnumTest : MonoBehaviour

{

    public EmAniType AniType;

}publicenum EmAniType

{

    Idle,

    Walk,

    Run,

    Atk,

    Hit,

    Die

}

4654621-77d8a69404254eab.png

为了将这些枚举值变成中文,这里使用了CustomPropertyDrawer(https://docs.unity3d.com/ScriptReference/CustomPropertyDrawer.html)。

        第一步,定义一个Unity属性标签PropertyAttribute。

using UnityEngine;publicclass EnumLabelAttribute : HeaderAttribute

{

    publicEnumLabelAttribute(stringheader) :base(header)

    {

    }

}

        这里没有继承PropertyAttribute,而是HeaderAttribute。原因是HeaderAttribute继承PropertyAttribute,而我想用到HeaderAttribute的header字段。当然我们也可以完全继承PropertyAttribute。

        第二步,使用CustomPropertyDrawer。在Editor文件夹下创建一个脚本EnumLabelDrawer.cs。EnumLabelDrawer继承PropertyDrawer,并加上CustomPropertyDrawer标签。在复写OnGUI方法,通过C#的反射,获取到枚举中枚举值上的Head标签属性数据。最终将这些属性中的中文说明展示出来。

using System.Collections.Generic;using UnityEditor;using UnityEngine;

[CustomPropertyDrawer(typeof(EnumLabelAttribute))]publicclass EnumLabelDrawer : PropertyDrawer

{

    privatereadonlyList m_displayNames =newList();

    publicoverridevoid OnGUI(Rect position, SerializedProperty property, GUIContent label)

    {

        varatt = (EnumLabelAttribute)attribute;

        vartype = property.serializedObject.targetObject.GetType();

        varfield = type.GetField(property.name);

        varenumtype = field.FieldType;

        foreach(varenumNamein property.enumNames)

        {

            varenumfield = enumtype.GetField(enumName);

            varhds = enumfield.GetCustomAttributes(typeof(HeaderAttribute),false);

            m_displayNames.Add(hds.Length <=0? enumName : ((HeaderAttribute)hds[0]).header);

        }

        EditorGUI.BeginChangeCheck();

        varvalue = EditorGUI.Popup(position, att.header, property.enumValueIndex, m_displayNames.ToArray());

        if (EditorGUI.EndChangeCheck())

        {

            property.enumValueIndex = value;

        }

    }

}

        第三步,更改原有的枚举和脚本字段。在枚举值上加上Header标签,在脚本的字段上增加EnumLabel标签。

using UnityEngine;publicclass EnumTest : MonoBehaviour

{

    [EnumLabel("动画类型")]

    public EmAniType AniType;

}publicenum EmAniType

{

    [Header("待机")]

    Idle,

    [Header("走")]

    Walk,

    [Header("跑")]

    Run,

    [Header("攻击")]

    Atk,

    [Header("受击")]

    Hit,

    [Header("死亡")]

    Die

}

4654621-4598c9910bd03aeb.gif

        看看效果

4654621-4c01f6013a81d7d7.png

猜你喜欢

转载自blog.csdn.net/weixin_34413357/article/details/86782065