Unity组合枚举的使用

5 篇文章 1 订阅

判断枚举中是否含义当前选择的枚举值

  1. 添加枚举的Editor扩展
using System;
using UnityEngine;

/// <summary>
/// 将枚举以位掩码的形式显示在Inspector上。
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class ShowAsFlagsAttribute : PropertyAttribute
{
}
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 自定义属性绘制器,将枚举以位掩码的形式显示在Inspector上。
/// </summary>
[CustomPropertyDrawer(typeof(ShowAsFlagsAttribute))]
public class ShowAsFlagsDrawer : PropertyDrawer
{
    private MethodInfo _miIntToEnumFlags;

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // 如果不是枚举,则按默认显示
        if (property.propertyType != SerializedPropertyType.Enum)
        {
            EditorGUI.PropertyField(position, property);
            return;
        }

        if (_miIntToEnumFlags == null)
        {
            _miIntToEnumFlags = typeof(EditorGUI).GetMethod("IntToEnumFlags", BindingFlags.Static | BindingFlags.NonPublic);
        }

        // 复杂的转换问题,让Unity来解决(参考EditorGUI.EnumFlagsField()方法的反编译结果)
        Enum currentEnum = (Enum)_miIntToEnumFlags.Invoke(null, new object[] { fieldInfo.FieldType, property.intValue });
        EditorGUI.BeginProperty(position, label, property);
        Enum newEnum = EditorGUI.EnumFlagsField(position, label, currentEnum);
        property.intValue = Convert.ToInt32(newEnum);
        EditorGUI.EndProperty();

        // 备注:
        // 不能使用以下方式获取枚举值:
        // Enum currentEnum = (Enum)fieldInfo.GetValue(property.serializedObject.targetObject);
        // 使用以下方式时,如果ScriptableObject中包含一个某类型的数组,该类型中包含了Flags枚举,将会导致Editor抛出ArgumentException:
        // ArgumentException: Field <enum_flags> defined on type <host_type> is not a field on the target object which is of type <unity_object>.
    }
}
  1. 测试组合枚举脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DayType : MonoBehaviour
{
    public Days days;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System;

/// <summary>
/// 组合枚举
/// </summary>
public class CombinationEnum : MonoBehaviour
{
    public Days days;

    public Transform parentDayEnum;

    private DayType[] dayTypes;
    void Start()
    {
        dayTypes = parentDayEnum.GetComponentsInChildren<DayType>(true);

        for (int i = 0; i < dayTypes.Length; i++)
        {
            //判断是否包含添加的权限
            if (days.HasFlag(dayTypes[i].days))
            {
                dayTypes[i].gameObject.SetActive(true);
            }
        }
    }
}
  1. unity的UI界面
    在这里插入图片描述
    在这里插入图片描述
    参考博客:
    https://arvin.blog.csdn.net/article/details/105594630
    https://blog.csdn.net/cai20142932/article/details/81107228
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值