Unity实现枚举类型中文显示
Unity脚本中枚举类型在inspector面板中文显示,供大家参考,具体内容如下
效果:
工具脚本:ChineseEnumTool.cs
usingSystem; usingUnityEngine; #ifUNITY_EDITOR usingUnityEditor; usingSystem.Reflection; usingSystem.Text.RegularExpressions; #endif //////设置枚举名称 /// #ifUNITY_EDITOR [AttributeUsage(AttributeTargets.Field)] #endif publicclassEnumAttirbute:PropertyAttribute { //////枚举名称 /// publicstringname; publicEnumAttirbute(stringname) { this.name=name; } } #ifUNITY_EDITOR [CustomPropertyDrawer(typeof(EnumAttirbute))] publicclassEnumNameDrawer:PropertyDrawer { publicoverridevoidOnGUI(Rectposition,SerializedPropertyproperty,GUIContentlabel) { //替换属性名称 EnumAttirbuteenumAttirbute=(EnumAttirbute)attribute; label.text=enumAttirbute.name; boolisElement=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); } } //////重新绘制枚举类型属性 /// ////// /// privatevoidDrawEnum(Rectposition,SerializedPropertyproperty,GUIContentlabel) { EditorGUI.BeginChangeCheck(); Typetype=fieldInfo.FieldType; string[]names=property.enumNames; string[]values=newstring[names.Length]; while(type.IsArray) { type=type.GetElementType(); } for(inti=0;i 新建Text脚本测试
usingSystem.Collections; usingSystem.Collections.Generic; usingUnityEngine; //定义动物类 publicenumAnimal { [EnumAttirbute("小狗")] dog, [EnumAttirbute("小猫")] cat, [EnumAttirbute("老虎")] tiger } publicclassTest:MonoBehaviour{ [EnumAttirbute("动物")] publicAnimalanimal; voidStart(){ } voidUpdate(){ } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。