Unity中如何设置枚举类型以显示中文值?
- 内容介绍
- 文章标签
- 相关推荐
本文共计659个文字,预计阅读时间需要3分钟。
在Unity脚本中,若要在Inspector面板中显示自定义类型,可以通过以下步骤实现:
1. 创建一个枚举类:定义一个枚举类型,用于存储需要显示在Inspector面板中的值。
2.编写工具脚本:创建一个工具脚本,用于将枚举类型转换为可在Inspector面板中显示的格式。
以下是一个简单的示例:
csharp
// ChineseEnumTool.csusing System;using UnityEngine;public class ChineseEnumTool : MonoBehaviour{ // 定义一个枚举类型,包含中文名称 public enum MyEnum { Option1=1, Option2=2, Option3=3 }
// 在Inspector面板中显示枚举值 [SerializeField] private MyEnum myEnum;
// 用于显示枚举值的函数 private string GetEnumDisplayName() { switch (myEnum) { case MyEnum.Option1: return 选项1; case MyEnum.Option2: return 选项2; case MyEnum.Option3: return 选项3; default: return 未知; } }
// 在Inspector面板中显示枚举的中文名称 void OnGUI() { GUILayout.Label(枚举值: + GetEnumDisplayName()); }}
效果:- 在Unity的Inspector面板中,将显示枚举值: 选项1等,根据实际的枚举值变化。
工具脚本:- `ChineseEnumTool.cs`:此脚本包含枚举定义和显示逻辑。
通过这种方式,你可以在Unity的Inspector面板中自定义显示任何枚举类型,而不需要额外的插件或复杂的设置。
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 () { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计659个文字,预计阅读时间需要3分钟。
在Unity脚本中,若要在Inspector面板中显示自定义类型,可以通过以下步骤实现:
1. 创建一个枚举类:定义一个枚举类型,用于存储需要显示在Inspector面板中的值。
2.编写工具脚本:创建一个工具脚本,用于将枚举类型转换为可在Inspector面板中显示的格式。
以下是一个简单的示例:
csharp
// ChineseEnumTool.csusing System;using UnityEngine;public class ChineseEnumTool : MonoBehaviour{ // 定义一个枚举类型,包含中文名称 public enum MyEnum { Option1=1, Option2=2, Option3=3 }
// 在Inspector面板中显示枚举值 [SerializeField] private MyEnum myEnum;
// 用于显示枚举值的函数 private string GetEnumDisplayName() { switch (myEnum) { case MyEnum.Option1: return 选项1; case MyEnum.Option2: return 选项2; case MyEnum.Option3: return 选项3; default: return 未知; } }
// 在Inspector面板中显示枚举的中文名称 void OnGUI() { GUILayout.Label(枚举值: + GetEnumDisplayName()); }}
效果:- 在Unity的Inspector面板中,将显示枚举值: 选项1等,根据实际的枚举值变化。
工具脚本:- `ChineseEnumTool.cs`:此脚本包含枚举定义和显示逻辑。
通过这种方式,你可以在Unity的Inspector面板中自定义显示任何枚举类型,而不需要额外的插件或复杂的设置。
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 () { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

