首先写一个EnumFlags类,下面的格式
using UnityEngine;
public class EnumFlags : PropertyAttribute { }
再写一个衍生类EnumFlagsAttributeDrawer类,按以下格式
using UnityEngine;
using UnityEditor;
[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);
}
}
然后实现我们的多选代码Tools类,按以下格式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using zFrame.Event;
using System;
/// <summary>
/// 定义枚举类型
/// </summary>
public enum MyEnum
{
// NONE,
one = 1 << 0, //<<运算符是代表可以共同存在,必须是这个写法,到时候判断实例出来的besine多选类型才能成功
two = 1 << 1,
there = 1 << 2,
four = 1 << 3,
}
public class Tools : MonoBehaviour
{
public Animator anim;
[EnumFlags] //这个是自己写的属性类,手动写的
public MyEnum besine;
// Use this for initialization
void Start()
{
swtchState(besine);
}
private void swtchState(MyEnum type)
{
if ((MyEnum.one & type) == MyEnum.one) //&判断type是否存在one这个枚举,与one做判断,
{
Debug.Log("我进入One环节");
}
if ((MyEnum.two & type) == MyEnum.two)
{
Debug.Log("我进入Two环节");
}
if ((MyEnum.there & type) == MyEnum.there)
{
Debug.Log("我进入There环节");
}
}
}