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 () {
 
 }
}

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

相关文章

  • WPF实现类似360安全卫士界面的程序源码分享

    WPF实现类似360安全卫士界面的程序源码分享

    最近在网上看到了新版的360安全卫士,感觉界面还不错,于是用WPF制作了一个,时间有限,一些具体的控件没有制作,用图片代替了。感兴趣的朋友一起跟着小编学习WPF实现类似360安全卫士界面的程序源码分享
    2015-09-09
  • C#使用自定义算法对数组进行反转操作的方法

    C#使用自定义算法对数组进行反转操作的方法

    这篇文章主要介绍了C#使用自定义算法对数组进行反转操作的方法,涉及C#针对数组操作的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • c#模拟银行atm机示例分享

    c#模拟银行atm机示例分享

    这篇文章主要介绍了c#模拟银行atm机示例,实现了用户登录、用户存款、用户取款等功能,需要的朋友可以参考下
    2014-03-03
  • C#实现TreeView节点拖拽的方法

    C#实现TreeView节点拖拽的方法

    这篇文章主要介绍了C#实现TreeView节点拖拽的方法,涉及C#针对TreeView节点的动态添加及移除技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • C#常用自定义函数小结

    C#常用自定义函数小结

    这篇文章主要介绍了C#常用自定义函数,包括将数组转成字符串、DateTime时间格式转换为Unix时间戳格式和生成某个范围内的随机数,需要的朋友可以参考下
    2014-09-09
  • C#用timer实现背单词小程序

    C#用timer实现背单词小程序

    这篇文章主要为大家详细介绍了C#用timer实现背单词小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • 超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)

    超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)

    正常情况下,我们是直接去string的length的,但是汉字是有两个字节的,所以直接用length是错的
    2018-03-03
  • C#编程实现自定义热键的方法

    C#编程实现自定义热键的方法

    这篇文章主要介绍了C#编程实现自定义热键的方法,涉及C#键盘按键设置的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • c# 实现发送邮件到指定邮箱

    c# 实现发送邮件到指定邮箱

    这篇文章主要介绍了c# 如何实现发送邮件到指定邮箱,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C#实现简单订单管理程序

    C#实现简单订单管理程序

    这篇文章主要为大家详细介绍了C#实现简单订单管理程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05

最新评论