Last active
January 11, 2018 08:30
-
-
Save mikenekoworks/31c2fde0fa3c562fa4596f1b924d1299 to your computer and use it in GitHub Desktop.
生のビット表示をしたり、編集したい貴方に。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[System.AttributeUsage( System.AttributeTargets.Field )] | |
public sealed class BitFlagsAttribute : PropertyAttribute { | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer( typeof( BitFlagsAttribute ) )] | |
public sealed class BitFlagsAttributeDrawer : PropertyDrawer { | |
int rowCount = 0; | |
public override void OnGUI( Rect position, SerializedProperty property, GUIContent label ) { | |
position.height = EditorGUIUtility.singleLineHeight; | |
// 数字の入力欄を作成 | |
EditorGUI.BeginChangeCheck(); | |
string value_string = EditorGUI.TextField( position, "Hexadecimal", property.longValue.ToString( "X" ) ); | |
if ( EditorGUI.EndChangeCheck() == true ) { | |
long parse_value = property.longValue; | |
if ( long.TryParse( value_string, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out parse_value ) == true ) { | |
property.longValue = parse_value; | |
} | |
} | |
position.y += EditorGUIUtility.singleLineHeight; | |
// ビットボタンを出力 | |
float left = position.xMin + EditorGUIUtility.singleLineHeight; | |
float width = position.width - ( EditorGUIUtility.singleLineHeight * 1.4f ); | |
float column_size = EditorGUIUtility.singleLineHeight * 2.0f; | |
float column4_space = column_size * 0.1f; | |
position.width = column_size; | |
int row_count = 4; | |
int column_count = 8; | |
if ( ( width / column_size ) > 16 ) { | |
row_count = 2; | |
column_count = 16; | |
} | |
if ( ( width / column_size ) > 32 ) { | |
row_count = 1; | |
column_count = 32; | |
} | |
rowCount = row_count; | |
float right = left + ( column_count - 1 ) * column_size + ( column4_space ) * ( column_count / 4 ); | |
position.x = right; | |
int index = 0; | |
uint bit_value = (uint)property.longValue; | |
for ( int i = 0; i < row_count; ++i ) { | |
for ( int n = 0; n < column_count; ++n ) { | |
bool bit = ( ( bit_value & ( 1 << index ) ) == 0 ) ? false : true; | |
EditorGUI.BeginChangeCheck(); | |
bool new_bit = GUI.Toggle( position, bit, index.ToString(), EditorStyles.miniButton ); | |
if ( EditorGUI.EndChangeCheck() == true ) { | |
if ( new_bit == true ) { | |
bit_value |= ( (uint)( 1 << index ) ); | |
} else { | |
bit_value &= ~( (uint)( 1 << index ) ); | |
} | |
} | |
position.x -= column_size; | |
if ( ( index % 4 ) == 3 ) { | |
position.x -= column4_space; | |
} | |
++index; | |
} | |
position.x = right; | |
position.y += EditorGUIUtility.singleLineHeight; | |
} | |
property.longValue = bit_value; | |
} | |
// プロパティを表示するのに必要な行の高さを返します。 | |
public override float GetPropertyHeight( SerializedProperty property, GUIContent label ) { | |
return EditorGUIUtility.singleLineHeight * ( rowCount + 1 ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Test { | |
[BitFlags] | |
public uint Flags; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment