【Unity3d】在编辑器中实现位操作(Bit)的编辑
2015-06-27 15:36
453 查看
有些时候,想高效利用数据,把一个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;
但后在编辑器看效果吧
以下是代码
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;
但后在编辑器看效果吧
相关文章推荐
- Kinect结合Unity3D引擎开发体感游戏(一)
- 一步一步跟我学易语言之第二个易程序菜单设计
- FCK编辑器(FCKEditor)添加新按钮和功能的修改方法
- ecshop后台编辑器替换成ueditor编辑器
- 10个基于jQuery或JavaScript的WYSIWYG 编辑器整理
- PHP网页 Ewebeditor 编辑器嵌入方法
- kindSoft在线网页编辑器简单的配置参数介绍
- dedecms5.5 最新版ckeditor编辑器整合教程
- javascript 在线文本编辑器实现代码
- 在线编辑器中换行与内容自动提取
- 网页上的Javascript编辑器和代码格式化
- Unity3D中脚本的执行顺序和编译顺序
- Unity3D动态对象优化代码分享
- 推荐5款跨平台的PHP编辑器
- unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk)
- Unity3D游戏引擎实现在Android中打开WebView的实例
- unity实现多点触控代码
- php单文件版在线代码编辑器
- dotnet封装的kindeditor编辑器控件