Unity实现枚举类型中文显示
作者:被代码折磨的狗子 时间:2023-02-22 12:00:28
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
标签:Unity,枚举类型,中文
0
投稿
猜你喜欢
解决Jenkins集成SonarQube遇到的报错问题
2023-11-24 08:54:10
Java GZip 基于内存实现压缩和解压的方法
2023-05-24 12:47:29
你所不知道的Spring自动注入详解
2021-09-04 19:30:08
JAVA多线程知识汇总
2021-08-03 09:04:47
Java通过What、Why、How了解弱引用
2021-11-01 00:06:20
Android实现简单的自定义ViewGroup流式布局
2022-03-16 10:21:23
详解C++ bitset用法
2022-10-30 08:57:16
android实现倒计时动态圈
2023-09-15 03:18:33
Java详细分析LCN框架分布式事务
2022-10-17 15:49:08
在winform下实现左右布局多窗口界面的方法
2023-02-23 11:31:51
Android应用中利用ViewPager实现多页面滑动切换效果示例
2023-01-27 04:32:19
android中Bitmap的放大和缩小实例代码
2021-10-01 13:23:28
模拟按Home键退出应用的简单方法(分享)
2023-10-30 20:02:36
C语言进制转换代码分享
2021-10-16 15:39:23
Android画板开发之基本画笔功能
2023-01-09 07:26:21
Java 设计模式中的命令模式详情
2023-11-15 23:25:33
详解C#中的out和ref
2023-08-13 18:15:21
Android Studio实现音乐播放器的全过程(简单易上手)
2023-07-10 08:02:43
Android搜索结果显示高亮实例(有数据滑动底部自动刷新)
2021-09-25 22:55:07
Android短信验证码倒计时验证的2种常用方式
2022-06-23 17:27:42