Unity實(shí)現(xiàn)枚舉類型中文顯示
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腳本測(cè)試
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//定義動(dòng)物類
public enum Animal
{
[EnumAttirbute("小狗")]
dog,
[EnumAttirbute("小貓")]
cat,
[EnumAttirbute("老虎")]
tiger
}
public class Test : MonoBehaviour {
[EnumAttirbute("動(dòng)物")]
public Animal animal;
void Start () {
}
void Update () {
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#使用EPPlus將圖片流嵌入到Excel實(shí)現(xiàn)示例
這篇文章主要為大家介紹了c#使用EPPlus將圖片流嵌入到Excel實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
C# 設(shè)計(jì)模式之單例模式歸納總結(jié)
這篇文章主要介紹了C#設(shè)計(jì)模式之單例模式實(shí)例講解,本文講解了單例模式的定義、單例模式的優(yōu)缺點(diǎn),需要的朋友可以參考下2017-04-04
教你C#將CSV轉(zhuǎn)為Excel的實(shí)現(xiàn)方法
這篇文章主要介紹了C#?將CSV轉(zhuǎn)為Excel,轉(zhuǎn)換之后可執(zhí)行更多關(guān)于數(shù)據(jù)編輯、格式設(shè)置等操作,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-03-03
跳一跳自動(dòng)跳躍C#代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了跳一跳自動(dòng)跳躍C#代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法,涉及C#多線程操作文件讀寫的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#使用csvhelper實(shí)現(xiàn)csv的基本操作
CsvHelper 是一個(gè)用于讀寫 CSV 文件的.NET庫,極其快速,靈活且易于使用,CsvHelper 建立在.NET Standard 2.0 之上,幾乎可以在任何地方運(yùn)行,本文給大家介紹了C#使用csvhelper實(shí)現(xiàn)csv的基本操作,需要的朋友可以參考下2024-07-07
c#異步操作后臺(tái)運(yùn)行(backgroundworker類)示例
這篇文章主要介紹了c#異步操作后臺(tái)運(yùn)行(backgroundworker類)示例,需要的朋友可以參考下2014-04-04

