zoukankan      html  css  js  c++  java
  • unity 多选枚举

    首先是自定义

    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;
        }
    }
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    HTML语义化
    使用全角空格进行文本对齐
    安卓手机微信中清除页面缓存的方法
    jQuery与Vue的对比
    IDE提交Git出现husky>pre-commit错误
    IDEA将工程转为maven工程
    vscode设置tab缩进字符数
    mac系统下用nginx服务器部署页面
  • 原文地址:https://www.cnblogs.com/luxishi/p/9004739.html
Copyright © 2011-2022 走看看