unity编辑器显示中文枚举

先上效果图

unity编辑器显示中文枚举_第1张图片


该类来自雨松http://www.xuanyusong.com/archives/4213

放在非 Editor 文件夹下

using UnityEngine;
using System;

[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
public class EnumLabelAttribute : PropertyAttribute
{
    public string label;
    public new 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;
    }
}


放在 Editor 文件夹下
下面这个才是我写哒

在这里显示枚举的中文Popup

using UnityEditor;
using System;
using System.Reflection;

public class EditorGUIEnumPopup : EditorWindow {
    
    public static object EnumPopup(string title, Enum selected)
    {
        int index = 0;
        var array = Enum.GetValues(selected.GetType());
        int length = array.Length;

        string[] enumString = new string[length];
        for (int i = 0; i < length; i++)
        {
            FieldInfo[] fields = selected.GetType().GetFields();
            foreach (FieldInfo field in fields)
            {
                if (field.Name.Equals(array.GetValue(i).ToString()))
                {
                    object[] objs = field.GetCustomAttributes(typeof(EnumLabelAttribute), true);
                    if (objs != null && objs.Length > 0)
                    {
                        enumString[i] = ((EnumLabelAttribute)objs[0]).label;
                    }
                }
            }
        }

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel(title);
        index = EditorGUILayout.Popup(selected.GetHashCode(), enumString);
        EditorGUILayout.EndHorizontal();

        return Enum.ToObject(selected.GetType(), index);
    }
}


你可能感兴趣的:(unity3d,unity3d,编辑器,枚举,中文)