【Unity3d】在编辑器中实现位操作(Bit)的编辑
来源:互联网 发布:迫击炮升级数据 编辑:程序博客网 时间:2024/05/16 02:05
有些时候,想高效利用数据,把一个int(32位)掰成32个bool值。那么编辑器中有没有一个这样的插件支持呢?
以下是代码
using UnityEngine;using UnityEditor;using System;using System.Collections;using System.Reflection;public enum MyEnum{Enum1 = 1,Enum2 = 2,}public class MyEnumFieldName{public const string Enum1 = @"第一位";public const string Enum2 = @"第一位";}public class BitMaskAttribute : PropertyAttribute{public BitMaskAttribute(){}}[CustomPropertyDrawer(typeof(BitMaskAttribute))]public class BitMaskDrawer : PropertyDrawer{ private float extraHeight = 20.0f; private static bool showValues = true; private bool hasError = false; public string GetFieldName(string enumName) { string className = string.Format("{0}FieldName", this.fieldInfo.FieldType.Name); Type classType = Type.GetType(className); if (classType != null) { FieldInfo fi = classType.GetField(enumName); if (fi != null) { return fi.GetValue(null).ToString(); } } return enumName; } public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label) { if (CheckInvalid(position, prop, label)) { return; } showValues = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, extraHeight), showValues, label); if (!showValues) { return; } EditorGUI.indentLevel++; for (int i = 0; i < prop.enumNames.Length; i++) { bool toggleValue = (prop.intValue & (1 << i)) == (1 << i); Rect rect = new Rect(position.x, position.y + ((i + 1) * extraHeight), position.width, extraHeight); string enumTitle = GetFieldName(prop.enumNames [i]); if (EditorGUI.Toggle(rect, enumTitle, toggleValue)) { prop.intValue |= 1 << i; } else { prop.intValue &= ~(1 << i); } } } private bool CheckInvalid(Rect position, SerializedProperty prop, GUIContent label) { if (prop.propertyType != SerializedPropertyType.Enum) { showValues = false; hasError = true; return true; } return false; } public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) { if (!showValues) { if (!hasError) { return base.GetPropertyHeight(prop, label); } else { return base.GetPropertyHeight(prop, label) + extraHeight; } } return base.GetPropertyHeight(prop, label) * (prop.enumNames.Length + 3); }}
用的时候很简单,只需要一句:
[BitMask] public MyEnum enumData;
但后在编辑器看效果吧
0 0
- 【Unity3d】在编辑器中实现位操作(Bit)的编辑
- 任意类型数的bit位操作
- 常用的Bit位操作 记录
- 常见的位操作bit manipulation
- unity3d 在编辑器中修改AnimationClip的属性值
- DataGrid的使用:(一)、在DataGrid控件中实现基本的操作(编辑、删除、分页)
- DataGrid的使用:(一)在DataGrid控件中实现基本的操作(编辑、删除、分页)
- [Unity3D]在Unity3D中实现简单的碰撞检测
- [Unity3D]在Unity3D中实现简单的碰撞检测
- BIT位操作用途
- (嵌入式)位(bit)操作
- Unity3d 64位编辑器中使用C/C++动态链接库的方法
- 给定一个整型变量a,写两段代码,第一个设置a的bit 3,第二个清除a 的bit 3。在以上两个操作中,要保持其它位不变。
- 实现对一个8bit数据指定位(例如第n位)的置0或置1操作
- Unity3d编辑器内部拖拽类操作的注意事项
- 在extjs中实现的文本编辑器
- 32bit win7 在VMWARE中安装64位的redhat LINUX4.7
- 在C语言中bit field 位域
- OJ矩阵之和
- 我的.Net技术体系
- SDWebImage 使用
- CSRF模拟post请求
- 黑马程序员-------Java概述
- 【Unity3d】在编辑器中实现位操作(Bit)的编辑
- linux 下的 vim 命令
- 利用tomcat发布WEB项目到内网和外网的方法
- 向Fragment传递数据
- 第十六周 项目一-平方根中的异常
- ios 启动程序时隐藏状态栏,启动后显示状态栏
- ruby-数字、字符串、数组
- android studio打包APK
- lejos+ev3+win7环境搭建