Unity编辑器开发,使用CustomPropertyDrawer实现枚举中文显示
2017-05-23 07:16
671 查看
在Unity开发中,枚举常常被用到。但是Unity自身对于枚举值,并不能做好中文的支持。无论是Head或者ToolTip.如下例:
为了将这些枚举值变成中文,这里使用了CustomPropertyDrawer(https://docs.unity3d.com/ScriptReference/CustomPropertyDrawer.html)。
第一步,定义一个Unity属性标签PropertyAttribute。
这里没有继承PropertyAttribute,而是HeaderAttribute。原因是HeaderAttribute继承PropertyAttribute,而我想用到HeaderAttribute的header字段。当然我们也可以完全继承PropertyAttribute。
第二步,使用CustomPropertyDrawer。在Editor文件夹下创建一个脚本EnumLabelDrawer.cs。EnumLabelDrawer继承PropertyDrawer,并加上CustomPropertyDrawer标签。在复写OnGUI方法,通过C#的反射,获取到枚举中枚举值上的Head标签属性数据。最终将这些属性中的中文说明展示出来。
第三步,更改原有的枚举和脚本字段。在枚举值上加上Header标签,在脚本的字段上增加EnumLabel标签。
看看效果
using UnityEngine; public class EnumTest : MonoBehaviour { public EmAniType AniType; } public enum EmAniType { Idle, Walk, Run, Atk, Hit, Die }
为了将这些枚举值变成中文,这里使用了CustomPropertyDrawer(https://docs.unity3d.com/ScriptReference/CustomPropertyDrawer.html)。
第一步,定义一个Unity属性标签PropertyAttribute。
using UnityEngine; public class EnumLabelAttribute : HeaderAttribute { public EnumLabelAttribute(string header) : base(header) { } }
这里没有继承PropertyAttribute,而是HeaderAttribute。原因是HeaderAttribute继承PropertyAttribute,而我想用到HeaderAttribute的header字段。当然我们也可以完全继承PropertyAttribute。
第二步,使用CustomPropertyDrawer。在Editor文件夹下创建一个脚本EnumLabelDrawer.cs。EnumLabelDrawer继承PropertyDrawer,并加上CustomPropertyDrawer标签。在复写OnGUI方法,通过C#的反射,获取到枚举中枚举值上的Head标签属性数据。最终将这些属性中的中文说明展示出来。
using System.Collections.Generic; using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(EnumLabelAttribute))] public class EnumLabelDrawer : PropertyDrawer { private readonly List<string> m_displayNames = new List<string>(); public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var att = (EnumLabelAttribute)attribute; var type = property.serializedObject.targetObject.GetType(); var field = type.GetField(property.name); var enumtype = field.FieldType; foreach (var enumName in property.enumNames) { var enumfield = enumtype.GetField(enumName); var hds = enumfield.GetCustomAttributes(typeof(HeaderAttribute), false); m_displayNames.Add(hds.Length <= 0 ? enumName : ((HeaderAttribute)hds[0]).header); } EditorGUI.BeginChangeCheck(); var value = EditorGUI.Popup(position, att.header, property.enumValueIndex, m_displayNames.ToArray()); if (EditorGUI.EndChangeCheck()) { property.enumValueIndex = value; } } }
第三步,更改原有的枚举和脚本字段。在枚举值上加上Header标签,在脚本的字段上增加EnumLabel标签。
using UnityEngine; public class EnumTest : MonoBehaviour { [EnumLabel("动画类型")] public EmAniType AniType; } public enum EmAniType { [Header("待机")] Idle, [Header("走")] Walk, [Header("跑")] Run, [Header("攻击")] Atk, [Header("受击")] Hit, [Header("死亡")] Die }
看看效果
相关文章推荐
- Unity编辑器开发,使用CustomEditor增加脚本的中文说明
- unity编辑器显示中文枚举
- JSP使用ckfinder实现中文图片上传后,无法显示,主要是tomcat不支持中文路径
- 使用POST表单处理方法,编写cgi和html文件,能够实现中文显示
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- Struts1应用、实现简单计算器、使用DispatchAction、显示友好的报错信息、使用动态Form简化开发
- EclipsePlug-in使用TextEditor开发自己的编辑器,实现关键字高亮和代码提示.
- android游戏开发框架libgdx的使用(三)--中文显示与汉字绘制
- android游戏开发框架libgdx的使用(三)--中文显示与汉字绘制
- cocos2d-x中文显示问题解决方法(使用cocos2dx中自带的iconv实现)
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- Struts1应用、实现简单计算器、使用DispatchAction、显示友好的报错信息、使用动态Form简化开发
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(二)----使用GUI.Box显示文字
- WindowsPhone-GameBoy模拟器开发五--使用XNA初略实现Gameboy显示系统
- 使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
- Spring自定义属性编辑器PropertyEditorSupport + 使用CustomEditorConfigurer注册属性编辑器
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(四)----使用PlayerPrefs存储数据到本地
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(五)----使用TextField 输入并调整排名
- 在Eclipse中使用Propertites Editor插件来解决property文件中文显示乱码
- ruby下使用iconv实现的中文显示