Skip to content

Instantly share code, notes, and snippets.

@N-Carter
Created September 9, 2014 12:08
Show Gist options
  • Save N-Carter/aec18b9965a9296cbd34 to your computer and use it in GitHub Desktop.
Save N-Carter/aec18b9965a9296cbd34 to your computer and use it in GitHub Desktop.
BitfieldPropertyDrawer
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomPropertyDrawer(typeof(BitfieldPropertyAttribute))]
public class BitfieldPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
GUIStyle style = "Button";
int bitfield = property.intValue;
int newBitfield = 0;
int bitWidth = 7;
int bitHeight = (int)position.height / 2;
EditorGUI.PrefixLabel(position, label);
int positionAfterPrefix = (int)(position.x + EditorGUIUtility.labelWidth);
int intFieldWidth = 60;
EditorGUI.LabelField(new Rect(positionAfterPrefix, position.y, intFieldWidth, position.height), bitfield.ToString("X8"));
for(int i = 0; i < 32; ++i)
{
Rect rect = new Rect(positionAfterPrefix + intFieldWidth + 4 + (bitWidth - 1) * (i % 16),
position.y + (bitHeight - 1) * (i / 16),
bitWidth, bitHeight);
int mask = 1 << (31 - i);
newBitfield |= (GUI.Toggle(rect, (bitfield & mask) != 0, GUIContent.none, style) ? mask : 0);
}
property.intValue = newBitfield;
EditorGUI.EndProperty();
}
/*
public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeight(property, label);
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment