Unity實(shí)現(xiàn)枚舉類型中文顯示
更新時間:2021年02月23日 16:42:06 作者:被代碼折磨的狗子
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)枚舉類型中文顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
Unity腳本中枚舉類型在inspector面板中文顯示,供大家參考,具體內(nèi)容如下
效果:

工具腳本:ChineseEnumTool.cs
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using System.Reflection;
using System.Text.RegularExpressions;
#endif
/// <summary>
/// 設(shè)置枚舉名稱
/// </summary>
#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class EnumAttirbute : PropertyAttribute
{
/// <summary>
/// 枚舉名稱
/// </summary>
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);
}
}
/// <summary>
/// 重新繪制枚舉類型屬性
/// </summary>
/// <param name="position"></param>
/// <param name="property"></param>
/// <param name="label"></param>
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
public class ChineseEnumTool : MonoBehaviour {
}
新建Text腳本測試
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//定義動物類
public enum Animal
{
[EnumAttirbute("小狗")]
dog,
[EnumAttirbute("小貓")]
cat,
[EnumAttirbute("老虎")]
tiger
}
public class Test : MonoBehaviour {
[EnumAttirbute("動物")]
public Animal animal;
void Start () {
}
void Update () {
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#客戶端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C#客戶端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
C#實(shí)現(xiàn)自定義動畫鼠標(biāo)的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)自定義動畫鼠標(biāo)效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#使用晚綁定來實(shí)現(xiàn)壓縮Access數(shù)據(jù)庫的方法
這篇文章主要介紹了C#使用晚綁定來實(shí)現(xiàn)壓縮Access數(shù)據(jù)庫的方法,項(xiàng)目開發(fā)中有一定的實(shí)用價值,需要的朋友可以參考下2014-08-08
Visual Studio C#創(chuàng)建windows服務(wù)程序
用Visual C#創(chuàng)建Windows服務(wù)不是一件困難的事,本文就將指導(dǎo)你一步一步創(chuàng)建一個Windows服務(wù)并使用它,本文主要介紹了Visual Studio C#創(chuàng)建windows服務(wù)程序,感興趣的可以了解一下2024-01-01

