Unity中显示中文属性(汉化你的Inspector)

汉化效果

利用Attribute可以自定义一些属性,实现汉化:

汉化效果图

用法示例

TitleTest.cs

using UnityEngine;
using UnityEngine.Events;

[System.Serializable]
public class Item {
    [Title("名称")]
    public string name;
    [EnumName("类型")]
    public Type type = Type.A;

    public enum Type {
        [EnumName("消耗品")]
        A,
        [EnumName("任务道具")]
        B
    }
}

/// <summary>
/// 标题属性测试
/// <para>ZhangYu 2018-06-21</para>
/// </summary>
public class TitleTest : MonoBehaviour {

    [Title("等级")]
    public int level;
    [Title("姓名")]
    public string name;
    [EnumName("武器")]
    public Weapon weapon;
    [EnumName("铠甲")]
    public Armor armor;
    [Title("道具列表", "yellow")]
    public Item[] items;
    [Title("死亡回调方法", "#FF0000")]
    public UnityEvent onDead;
    [Title("复活回调方法", "#00FF00")]
    public UnityEvent onRevive;

    public enum Weapon {
        [EnumName("蛇矛")]
        A,
        [EnumName("胜宗刀")]
        B,
        [EnumName("青缸剑")]
        C,
        [EnumName("李广弓")]
        D
    }

    public enum Armor {
        [EnumName("钢甲")]
        One,
        [EnumName("火焰甲")]
        Two,
        [EnumName("青龙甲")]
        Three,
        [EnumName("藤甲")]
        Four
    }

    void Start () {
        
    }
    
}

Attribute脚本

TitleAttribute.cs

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

/// <summary>
/// 标题属性
/// <para>ZhangYu 2018-06-21</para>
/// </summary>
#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class TitleAttribute : PropertyAttribute {

    /// <summary> 标题名称 </summary>
    public string title;
    /// <summary> 标题颜色 </summary>
    public string htmlColor;

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

}

#if UNITY_EDITOR
[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;
    }

    /// <summary> Html颜色转换为Color </summary>
    /// <param name="hex"></param>
    /// <returns></returns>
    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;
        }
    }

}
#endif

/// <summary>
/// 设置枚举名称
/// <para>ZhangYu 2018-06-21</para>
/// </summary>

#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class EnumNameAttribute : PropertyAttribute {

    /// <summary> 枚举名称 </summary>
    public string name;

    public EnumNameAttribute(string name) {
        this.name = name;
    }

}

#if UNITY_EDITOR
[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;
    }

}
#endif

PS1:感谢雨松momo和他的文章:《Unity3D研究院之Inspector面板枚举的别名与排序(八十九)》
PS2:转载请注明原文地址:https://segmentfault.com/a/11...

要在Unity Inspector显示字典,需要使用Unity内置的序列化功能,并为字典创建一个自定义的Editor。 以下是一个示例代码,演示如何创建一个自定义的Editor,以在Inspector显示字典: ```csharp using UnityEngine; using UnityEditor; using System.Collections.Generic; [CustomEditor(typeof(MyScript))] public class MyScriptEditor : Editor { private Dictionary<string, int> myDictionary; void OnEnable() { myDictionary = new Dictionary<string, int>(); } public override void OnInspectorGUI() { serializedObject.Update(); SerializedProperty dictionaryProp = serializedObject.FindProperty("myDictionary"); if (dictionaryProp != null) { EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(dictionaryProp, true); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); } } base.OnInspectorGUI(); } } ``` 在上述代码,MyScript是你要使用字典的脚本。在OnEnable()方法,我们初始化了字典。 在OnInspectorGUI()方法,我们首先使用serializedObject.Update()来更新序列化对象,然后使用serializedObject.FindProperty()方法找到我们要显示的字典属性。然后,我们使用EditorGUILayout.PropertyField()方法来显示字典属性,第二个参数为true表示我们要显示字典的所有元素。最后,我们使用EditorGUI.EndChangeCheck()方法和serializedObject.ApplyModifiedProperties()方法来保存对字典的修改。 请注意,在使用自定义编辑器时,你需要在脚本类上使用[CustomEditor(typeof(MyScript))]属性进行标记,其MyScript是你要使用字典的脚本类的名称。 希望这可以帮助你在Unity Inspector显示字典!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值