unity C#获得枚举(enum)类型的长度

博客提及了C#和Unity,C#是一种广泛应用的编程语言,Unity则是知名的游戏开发引擎,二者在软件开发尤其是游戏开发领域常结合使用。

C#

enum TestEnum
{
    Type1,
    Type2,
}

class Program
{
    static void Main(string[] args)
    {
        var e = new TestEnum();
        string[] EnumStr= System.Enum.GetNames(e.GetType());
    }
}

unity

  void Start()
    {
        string[] EnumStr= Enum.GetNames(e.GetType());
    }
Unity 中,将 C# 枚举类型的参数暴露在 Inspector 面板中进行配置是一项常见需求。默认情况下,Unity 会自动将 `public` 枚举字段显示在 Inspector 面板中。此外,还可以通过一些特性(Attribute)和自定义标签来增强枚举的可读性和排序方式。 ### 暴露枚举参数的基本方法 1. **使用 public 访问修饰符** 默认情况下,Unity 会自动将 `public` 的枚举字段显示在 Inspector 面板上: ```csharp public enum ObjectTypeEnum { General, Bridge, Road } public class ObjectDescribe : MonoBehaviour { public ObjectTypeEnum objectType; } ``` 这样就可以直接在 Inspector 面板中看到并修改 `objectType` 的值 [^4]。 2. **使用 [SerializeField] 特性隐藏字段但仍然暴露给 Inspector** 如果需要保持字段为 `private` 或 `protected`,但仍希望其在 Inspector 中可见,则可以使用 `[SerializeField]` 特性: ```csharp [System.Serializable] public enum ObjectTypeEnum { General, Bridge, Road } public class ObjectDescribe : MonoBehaviour { [SerializeField] private ObjectTypeEnum objectType; } ``` 此时 `objectType` 字段仍然是私有的,但可以在 Inspector 中编辑 [^2]。 ### 自定义枚举的显示名称 为了提高可读性,可以通过自定义特性为枚举值添加中文或更友好的显示名称。例如,可以创建一个自定义特性类 `EnumLabelAttribute` 来设置每个枚举值的别名: ```csharp [System.AttributeUsage(System.AttributeTargets.Field)] public class EnumLabelAttribute : System.Attribute { public string Label { get; private set; } public int Order { get; private set; } public EnumLabelAttribute(string label, int order = 0) { Label = label; Order = order; } } ``` 然后在枚举中应用该特性: ```csharp public enum NewType : byte { [EnumLabel("我是1")] One = 10, [EnumLabel("我是2")] Two = 1, [EnumLabel("我是3")] Three = 5, [EnumLabel("我是4")] Four = 2 } ``` 最后,在脚本中引用该枚举: ```csharp public class NewBehaviourScript : MonoBehaviour { [EnumLabel("我是的类型", 10, 1, 5, 2)] public NewType newType = NewType.One; } ``` 这样,Inspector 面板中就会显示自定义的别名,而不是默认的枚举名称 [^1]。 ### 控制枚举的排序顺序 如果需要调整 Inspector 面板中枚举值的显示顺序,可以通过在特性中传递排序参数实现。例如,在 `EnumLabelAttribute` 中增加一个 `Order` 参数,并在自定义 Editor 脚本中处理排序逻辑: ```csharp [CustomPropertyDrawer(typeof(NewType))] public class EnumLabelDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // 获取所有枚举值及其对应的标签和排序值 var enumValues = typeof(NewType).GetFields() .Where(f => f.GetCustomAttributes(typeof(EnumLabelAttribute), false).Length > 0) .Select(f => new { Name = f.Name, Value = (NewType)Enum.Parse(typeof(NewType), f.Name), Attribute = (EnumLabelAttribute)f.GetCustomAttributes(typeof(EnumLabelAttribute), false)[0] }) .OrderBy(e => e.Attribute.Order) .ToList(); // 创建下拉菜单选项 List<GUIContent> options = new List<GUIContent>(); foreach (var item in enumValues) { options.Add(new GUIContent(item.Attribute.Label)); } // 绘制下拉菜单 int selectedIndex = enumValues.FindIndex(e => e.Value.Equals(property.enumValueIndex)); selectedIndex = EditorGUI.Popup(position, label, selectedIndex, options.ToArray()); if (selectedIndex >= 0 && selectedIndex < enumValues.Count) { property.enumValueIndex = (int)enumValues[selectedIndex].Value; } } } ``` 通过这种方式,可以灵活地控制 Inspector 面板中枚举值的显示顺序 [^1]。 ### 总结 - 使用 `public` 枚举字段可以直接在 Inspector 中显示。 - 使用 `[SerializeField]` 可以让私有字段也出现在 Inspector 中。 - 使用自定义特性如 `EnumLabelAttribute` 可以为枚举值添加别名,提高可读性。 - 通过编写自定义 `PropertyDrawer` 可以控制枚举值的排序和显示方式。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值