Unity 编辑器属性面板字段和枚举显示中文、带颜色标题

示例.jpg

使用方法:


使用方法.png

主要3个文件:

  • FieldLabelDrawer;
  • EnumAttirbute;
  • InspectorShowCN;

FieldLabelDrawer代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

//将属性名显示成中文
[CustomPropertyDrawer(typeof(InspectorShow))]
public class FieldLabelDrawer : PropertyDrawer
{
    private InspectorShow FLAttribute
    {
        get { return (InspectorShow)attribute; }
    }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //在这里重新绘制
        EditorGUI.PropertyField(position, property, new GUIContent(FLAttribute.label), true);

    }
}

//显示颜色
[CustomPropertyDrawer(typeof(TitleAttribute))]
public class TitleAttributeDrawer : DecoratorDrawer
{
    // 文本样式
    private GUIStyle style = new GUIStyle();

    public override void OnGUI(Rect position)
    {
        // 获取Attribute
        TitleAttribute attr = (TitleAttribute)attribute;

        // 转换颜色
        Color color = htmlToColor(attr.htmlColor);

        // 重绘GUI
        position = EditorGUI.IndentedRect(position);
        style.normal.textColor = color;
        GUI.Label(position, attr.title, style);
    }

    public override float GetHeight()
    {
        return base.GetHeight() - 4;
    }

    ///  Html颜色转换为Color 
    /// 
    /// 
    private Color htmlToColor(string hex)
    {
        // 默认黑色
        if (string.IsNullOrEmpty(hex)) return Color.black;

        // 转换颜色
        hex = hex.ToLower();
        if (hex.IndexOf("#") == 0 && hex.Length == 7)
        {
            int r = Convert.ToInt32(hex.Substring(1, 2), 16);
            int g = Convert.ToInt32(hex.Substring(3, 2), 16);
            int b = Convert.ToInt32(hex.Substring(5, 2), 16);
            return new Color(r / 255f, g / 255f, b / 255f);
        }
        else if (hex == "red")
        {
            return Color.red;
        }
        else if (hex == "green")
        {
            return Color.green;
        }
        else if (hex == "blue")
        {
            return Color.blue;
        }
        else if (hex == "yellow")
        {
            return Color.yellow;
        }
        else if (hex == "black")
        {
            return Color.black;
        }
        else if (hex == "white")
        {
            return Color.white;
        }
        else if (hex == "cyan")
        {
            return Color.cyan;
        }
        else if (hex == "gray")
        {
            return Color.gray;
        }
        else if (hex == "grey")
        {
            return Color.grey;
        }
        else if (hex == "magenta")
        {
            return Color.magenta;
        }
        else
        {
            return Color.black;
        }
    }

}

//显示中文枚举
[CustomPropertyDrawer(typeof(EnumNameAttribute))]
public class EnumNameDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // 替换属性名称
        EnumNameAttribute rename = (EnumNameAttribute)attribute;
        label.text = rename.name;

        // 重绘GUI
        bool isElement = Regex.IsMatch(property.displayName, "Element \\d+");
        if (isElement) label.text = property.displayName;
        if (property.propertyType == SerializedPropertyType.Enum)
        {
            drawEnum(position, property, label);
        }
        else
        {
            EditorGUI.PropertyField(position, property, label, true);
        }
    }

    // 绘制枚举类型
    private void drawEnum(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginChangeCheck();

        // 获取枚举相关属性
        Type type = fieldInfo.FieldType;
        string[] names = property.enumNames;
        string[] values = new string[names.Length];
        while (type.IsArray) type = type.GetElementType();

        // 获取枚举所对应的名称
        for (int i = 0; i < names.Length; i++)
        {
            FieldInfo info = type.GetField(names[i]);
            EnumNameAttribute[] atts = (EnumNameAttribute[])info.GetCustomAttributes(typeof(EnumNameAttribute), false);
            values[i] = atts.Length == 0 ? names[i] : atts[0].name;
        }

        // 重绘GUI
        int index = EditorGUI.Popup(position, label.text, property.enumValueIndex, values);
        if (EditorGUI.EndChangeCheck() && index != -1) property.enumValueIndex = index;
    }
}

EnumAttirbute代码:

using System;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
using System.Reflection;
using System.Text.RegularExpressions;
#endif


/// 
/// 设置枚举名称
/// 
#if UNITY_EDITOR
    [AttributeUsage(AttributeTargets.Field)]
#endif
public class EnumAttirbute : PropertyAttribute
{
    /// 
    /// 枚举名称
    /// 
    public string name;
    public EnumAttirbute(string name)
    {
        this.name = name;
    }
}

#if UNITY_EDITOR
    [CustomPropertyDrawer(typeof(EnumAttirbute))]
    public class EnumNameDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // 替换属性名称
            EnumAttirbute enumAttirbute = (EnumAttirbute)attribute;
            label.text = enumAttirbute.name;

            bool isElement = Regex.IsMatch(property.displayName, "Element \\d+");
            if (isElement)
            {
                label.text = property.displayName;
            }

            if (property.propertyType == SerializedPropertyType.Enum)
            {
                DrawEnum(position, property, label);
            }
            else
            {
                EditorGUI.PropertyField(position, property, label, true);
            }
        }

        /// 
        /// 重新绘制枚举类型属性
        /// 
        /// 
        /// 
        /// 
        private void DrawEnum(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginChangeCheck();
            Type type = fieldInfo.FieldType;

            string[] names = property.enumNames;
            string[] values = new string[names.Length];
            while (type.IsArray)
            {
                type = type.GetElementType();
            }

            for (int i = 0; i < names.Length; ++i)
            {
                FieldInfo info = type.GetField(names[i]);
                EnumAttirbute[] enumAttributes = (EnumAttirbute[])info.GetCustomAttributes(typeof(EnumAttirbute), false);
                values[i] = enumAttributes.Length == 0 ? names[i] : enumAttributes[0].name;
            }

            int index = EditorGUI.Popup(position, label.text, property.enumValueIndex, values);
            if (EditorGUI.EndChangeCheck() && index != -1)
            {
                property.enumValueIndex = index;
            }
        }
    }
#endif

InspectorShow代码:

using System;
using UnityEngine;

//显示中文
[AttributeUsage(AttributeTargets.Field)]
public class InspectorShow : PropertyAttribute
{
    public string label;        //要显示的字符

    /// 
    /// 属性面板显示中文
    /// 
    /// 
    public InspectorShow(string label)
    {
        this.label = label;
    }
}

//显示颜色
public class TitleAttribute : PropertyAttribute
{
    ///  标题名称 
    public string title;
    ///  标题颜色 
    public string htmlColor;

    ///  在属性上方添加一个标题 
    /// 标题名称
    /// 标题颜色
    public TitleAttribute(string title, string htmlColor = "#FFFFFF")
    {
        this.title = title;
        this.htmlColor = htmlColor;
    }
}


//显示枚举名称
[AttributeUsage(AttributeTargets.Field)]
public class EnumNameAttribute : PropertyAttribute
{
    ///  枚举名称 
    public string name;
    public new int[] order = new int[0];

    /// 
    /// 枚举属性面板显示中文
    /// 
    /// 
    public EnumNameAttribute(string name)
    {
        this.name = name;
    }

    public EnumNameAttribute(string label, params int[] order)
    {
        this.name = label;
        this.order = order;
    }
}

你可能感兴趣的:(Unity 编辑器属性面板字段和枚举显示中文、带颜色标题)