Unity实现枚举类型中文显示

 更新时间:2021年02月23日 16:42:06   作者:被代码折磨的狗子  
这篇文章主要为大家详细介绍了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 () {
 
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • c#使用EPPlus将图片流嵌入到Excel实现示例

    c#使用EPPlus将图片流嵌入到Excel实现示例

    这篇文章主要为大家介绍了c#使用EPPlus将图片流嵌入到Excel实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • C# 设计模式之单例模式归纳总结

    C# 设计模式之单例模式归纳总结

    这篇文章主要介绍了C#设计模式之单例模式实例讲解,本文讲解了单例模式的定义、单例模式的优缺点,需要的朋友可以参考下
    2017-04-04
  • c# mutex互斥量的深入解析

    c# mutex互斥量的深入解析

    本篇文章主要是对c#中的mutex互斥量进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • 教你C#将CSV转为Excel的实现方法

    教你C#将CSV转为Excel的实现方法

    这篇文章主要介绍了C# 将CSV转为Excel,转换之后可执行更多关于数据编辑、格式设置等操作,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2022-03-03
  • c#日志记录帮助类分享

    c#日志记录帮助类分享

    这篇文章主要介绍了c#日志记录帮助类,可以设置记录的日志类型,需要的朋友可以参考下
    2014-03-03
  • 跳一跳自动跳跃C#代码实现

    跳一跳自动跳跃C#代码实现

    这篇文章主要为大家详细介绍了跳一跳自动跳跃C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • C#实现多线程写入同一个文件的方法

    C#实现多线程写入同一个文件的方法

    这篇文章主要介绍了C#实现多线程写入同一个文件的方法,涉及C#多线程操作文件读写的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • C#使用csvhelper实现csv的基本操作

    C#使用csvhelper实现csv的基本操作

    CsvHelper 是一个用于读写 CSV 文件的.NET库,极其快速,灵活且易于使用,CsvHelper 建立在.NET Standard 2.0 之上,几乎可以在任何地方运行,本文给大家介绍了C#使用csvhelper实现csv的基本操作,需要的朋友可以参考下
    2024-07-07
  • C#中POST接口formdata传参模板的记录

    C#中POST接口formdata传参模板的记录

    这篇文章主要介绍了C#中POST接口formdata传参模板的记录方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • c#异步操作后台运行(backgroundworker类)示例

    c#异步操作后台运行(backgroundworker类)示例

    这篇文章主要介绍了c#异步操作后台运行(backgroundworker类)示例,需要的朋友可以参考下
    2014-04-04

最新评论