简介:
在编辑器中,可以很简单的实现枚举的多选。
方法一
在脚本中自定义枚举属性
| using System; |
| using UnityEngine; |
| |
| |
| |
| public enum EnumType |
| { |
| One, |
| Two, |
| Three, |
| Four |
| } |
| |
| |
| |
| public class EnumMultiAttribute : PropertyAttribute { } |
| public class Test : MonoBehaviour |
| { |
| [Header("测试枚举")] |
| [EnumMultiAttribute] |
| public EnumType enumType; |
| } |
在Editor
文件夹创建属性脚本
| using UnityEditor; |
| using UnityEngine; |
| |
| |
| |
| |
| [CustomPropertyDrawer(typeof(EnumMultiAttribute))] |
| public class EnumMultiAttributeDrawer : PropertyDrawer |
| { |
| public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) |
| { |
| property.intValue = EditorGUI.MaskField(position, label, property.intValue, property.enumNames); |
| } |
| } |
如果需要判断是否选中了某个枚举,需要位移操作
| |
| public bool IsSelectEnumType(EnumType type) |
| { |
| int index = 1 << (int)type; |
| int eventTypeResult = (int)enumType; |
| if ((eventTypeResult & index) == index) |
| { |
| return true; |
| } |
| return false; |
| } |
方法二
| using System; |
| using UnityEngine; |
| |
| [Flags] |
| public enum Week |
| { |
| None = 0, |
| Monday = 1, |
| Tuesday = 2, |
| Wednesday = 4, |
| Thursday = 8, |
| Friday = 16, |
| Saturday = 32, |
| Sunday = 64 |
| } |
| public class Test : MonoBehaviour |
| { |
| public Week weekDay; |
| |
| |
| |
| |
| |
| public static void Add(ref Week w, Week[] _wArray) |
| { |
| foreach (Week _w in _wArray) |
| { |
| w = w | _w; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| public static void Remove(ref Week w, Week[] _wArray) |
| { |
| foreach (Week _w in _wArray) |
| { |
| w = w & ~_w; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| public static bool IsContain(Week w, Week _w) |
| { |
| return 0 != (w & _w); |
| } |
| |
| |
| |
| |
| public static bool IsNone(Week w) |
| { |
| return w != Week.None; |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律