Unity实现枚举类型中文显示
作者:被代码折磨的狗子 发布时间:2023-02-22 12:00:28
标签:Unity,枚举类型,中文
Unity脚本中枚举类型在inspector面板中文显示,供大家参考,具体内容如下
效果:
工具脚本:ChineseEnumTool.cs
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using System.Reflection;
using System.Text.RegularExpressions;
#endif
/// <summary>
/// 设置枚举名称
/// </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 () {
}
}
来源:https://blog.csdn.net/qq_42345116/article/details/113944092


猜你喜欢
- 一、开篇通过对之前Java之路的了解之后,相信初学者们都对Java有了一个比较深印象的了解了。但是事情不能总停留在理论层面,还得多多实现,才
- 本文实例讲述了C#异步执行任务的方法。分享给大家供大家参考。具体如下:// 异步执行耗时任务(适合不需要等它的执行结果的场景,如发邮件、发短
- \r与\n到底有何区别,编码的时候又应该如何使用,我们下面来了解一下。区别:\r:全称:carriage return (carriage是
- 一:@@的意思是以@标注的字符出,其中所有的符号均为字符串符号,没有什么特殊字符,如''什么的,均默认为字符串
- 一般在android显示一个View都是通过Activity的setContentView设置的,但是还有一种方法,可以直接使用Window
- 依赖倒置原则(DIP)定义:高层模块不应该依赖低层模块,二者都应该依赖其抽象;抽象不应该依赖细节;细节应该依赖抽象。问题由来:类A直接依赖类
- 引言相信大家在生活中,特别是最近的双十一活动期间,会收到很多短信,而那些短信都有两个特征,第一个是几乎都是垃圾短信,这个特点此处可以忽略不计
- null与voidnull值用来表示数据类型未被赋予任何值,它是一种引用类型;void表示没有类型,或者说是没有任何值。null与void的
- 需求说明实现方式嗯 这个可以视作一个经典的消费者和生产者的问题,详细见代码注释代码内容 消费者,负责取走生产者产生的信息/** * @aut
- 前言本文准确来讲是探讨如何用 Jackson 来序列化 Apache avro 对象,因为简单用 Jackson 来序列化 Apache a
- public static string EncryptWithMD5(string source) &n
- Kotlin 基础教程之类、对象、接口Kotlin中类、接口相关概念与Java一样,包括类名、属性、方法、继承等,如下示例:interfac
- 前言在java中遍历Map有不少的方法。这篇文章我们就来看一下Java读取Map的两种方法以及这两种方法的对比。一、 遍历Map方
- 目录前言connectTimeout:callTimeout:pingIntervalwriteTimeoutreadTimeout总结前言
- 在之前的博客中已经为大家介绍了,如何在win环境下配置DNK程序,本篇我将带大家实现一个简单的Hello jni程序,让大家真正感受一下ND
- 有httponly的cookie,在httpwebreqeust请求时,会获取不到,可以采用直接获取head中的set-cookie,再转换
- 前言在工作中,比如要实现一个功能,前端传什么参数,后端的controller层中怎么接收参数 ,封装成了什么实体对象,有些参数是在URL上使
- Double转化为String时的保留位数及格式有时需要将程序中的数据写入到文件中进行保存,这时候就涉及到数据的字符串格式问题。下面介绍Do
- 前言含义:(1)多重循环指一个循环语句的循环体中再包含循环语句,又称嵌套循环。(2)循环语句内可以嵌套多层循环。(3)不同的循环语句可以相互
- 通常C#使用基于XML的配置文件,不过如果有需要的话,比如要兼顾较老的系统,可能还是要用到INI文件。但C#本身并不具备读写INI文件的AP