Enum的多选、选择判断及遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/n_moling/article/details/88679875

1、多选

public class EnumFlags : PropertyAttribute { }
[CustomPropertyDrawer(typeof(EnumFlags))]
public class EnumFlagsAttributeDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        property.intValue = EditorGUI.MaskField(position, label, property.intValue, property.enumNames);
    }
}

2、选择判断

private bool IsSelectEventType(EvidenceAttribute _eventType)
{
    // 将枚举值转换为int 类型, 1 左移 
    int index = 1 << (int)_eventType;
    // 获取所有选中的枚举值
    int eventTypeResult = (int)evidenceAttribute;
    // 按位 与
    if ((eventTypeResult & index) == index)
    {
        return true;
    }
    return false;
}

3、遍历

foreach (EvidenceAttribute item in Enum.GetValues(typeof(EvidenceAttribute)))
{
    if (IsSelectEventType(item))
    {
        //DoSomesing
    }
}

猜你喜欢

转载自blog.csdn.net/n_moling/article/details/88679875