Advertisement
ChestnutGames

Unity3D BitMaskAttribute

Jul 30th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public static class EditorExtension
  6. {
  7.     public static int DrawBitMaskField (Rect aPosition, int aMask, System.Type aType, GUIContent aLabel)
  8.     {
  9.         var itemNames = System.Enum.GetNames(aType);
  10.         var itemValues = System.Enum.GetValues(aType) as int[];
  11.        
  12.         int val = aMask;
  13.         int maskVal = 0;
  14.         for(int i = 0; i < itemValues.Length; i++)
  15.         {
  16.             if (itemValues[i] != 0)
  17.             {
  18.                 if ((val & itemValues[i]) == itemValues[i])
  19.                     maskVal |= 1 << i;
  20.             }
  21.             else if (val == 0)
  22.                 maskVal |= 1 << i;
  23.         }
  24.         int newMaskVal = EditorGUI.MaskField(aPosition, aLabel, maskVal, itemNames);
  25.         int changes = maskVal ^ newMaskVal;
  26.        
  27.         for(int i = 0; i < itemValues.Length; i++)
  28.         {
  29.             if ((changes & (1 << i)) != 0)            // has this list item changed?
  30.             {
  31.                 if ((newMaskVal & (1 << i)) != 0)     // has it been set?
  32.                 {
  33.                     if (itemValues[i] == 0)           // special case: if "0" is set, just set the val to 0
  34.                     {
  35.                         val = 0;
  36.                         break;
  37.                     }
  38.                     else
  39.                         val |= itemValues[i];
  40.                 }
  41.                 else                                  // it has been reset
  42.                 {
  43.                     val &= ~itemValues[i];
  44.                 }
  45.             }
  46.         }
  47.         return val;
  48.     }
  49. }
  50.  
  51. [CustomPropertyDrawer(typeof(BitMaskAttribute))]
  52. public class EnumBitMaskPropertyDrawer : PropertyDrawer
  53. {
  54.     public override void OnGUI (Rect position, SerializedProperty prop, GUIContent label)
  55.     {
  56.         var typeAttr = attribute as BitMaskAttribute;
  57.         // Add the actual int value behind the field name
  58.         label.text = label.text + "("+prop.intValue+")";
  59.         prop.intValue = EditorExtension.DrawBitMaskField(position, prop.intValue, typeAttr.propType, label);
  60.     }
  61. }
  62.  
  63. public class BitMaskAttribute : PropertyAttribute
  64. {
  65.     public System.Type propType;
  66.     public BitMaskAttribute(System.Type aType)
  67.     {
  68.         propType = aType;
  69.     }
  70. }
  71.  
  72. /* How to use it:
  73. / [BitMaskAttribute(typeof(WalkableTile.Direction))]
  74. /  public WalkableTile.Direction direction;
  75. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement