首先是自定义
using UnityEngine;
using System.Collections;
using UnityEditor;
public class EnumFlagsAttribute : PropertyAttribute {}
[CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
public class EnumFlagsAttributeDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
/*
* 绘制多值枚举选择框,0 全部不选, -1 全部选中, 其他是枚举之和
* 枚举值 = 当前下标值 ^ 2
* 默认[0^2 = 1 , 1 ^2 = 2, 4, 16 , .....]
*/
property.intValue = EditorGUI.MaskField(position, label, property.intValue
, property.enumNames);
}
}
然后是脚本
using UnityEngine;
using System.Collections;
public enum LayerMaskEnum
{
Layer1,
Layer2,
Layer3,
Layer4,
Layer5,
Layer6,
Layer7,
Layer8,
Layer9,
Layer10
}
public class MyWdsCompoment : MonoBehaviour {
[EnumFlagsAttribute]
public LayerMaskEnum layer;
[ContextMenu("输出Layer值")]
public void DebugPrint()
{
Debug.Log("layer="+(int)layer);
Debug.Log(IsSelectEventType(LayerMaskEnum.Layer10));
}
//判断是否选择了该枚举值
public bool IsSelectEventType(LayerMaskEnum _eventType)
{
// 将枚举值转换为int 类型, 1 左移
int index = 1 << (int)_eventType;
// 获取所有选中的枚举值
int eventTypeResult = (int)layer;
// 按位 与
if ((eventTypeResult & index) == index)
{
return true;
}
return false;
}
}