CodingJar

Enumeration Flags Support for Unity

Nov 10th, 2014
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. // ---
  2. // This is EnumFlagsFieldAttribute.cs
  3. // ---
  4. using UnityEngine;
  5.  
  6. namespace CodingJar
  7. {
  8.     /// <summary>
  9.     /// Allow an enumeration to select multiple values (bitfield, using System.Flags).
  10.     /// </summary>
  11.     public class EnumFlagsFieldAttribute : PropertyAttribute {}
  12. }
  13.  
  14. // ---
  15. // This is EnumFlagsAttributeDrawer.cs
  16. // This file needs to be placed in an "Editor" directory.
  17. // ---
  18. using UnityEngine;
  19. using UnityEditor;
  20.  
  21. namespace CodingJar
  22. {
  23.     /// <summary>
  24.     /// This allows us to use an Enum as a masking field (just like the LayerMask in Unity).
  25.     /// </summary>
  26.  
  27.     /// <example>
  28.     ///
  29.     /// // First define the Enumeration type and follow .NET-friendly practice of specifying it with System.FlagsAttribute.
  30.     /// [System.Flags]
  31.     /// enum MyFlagType
  32.     /// {
  33.     ///     Zero = 0,
  34.     ///     One = (1 << 0),
  35.     ///     Two = (1 << 1),
  36.     ///     Three = (1 << 2)
  37.     /// }
  38.     ///
  39.     /// // Now define the actual field, and tag on the EnumFlagsFieldAttribute.
  40.     /// [SerializeField, EnumFlagsField] MyFlagType     _fieldName;
  41.     ///
  42.     /// </example>
  43.  
  44.     [CustomPropertyDrawer(typeof(EnumFlagsFieldAttribute))]
  45.     class EnumFlagsAttributeDrawer : PropertyDrawer
  46.     {
  47.         private System.Type     _objectType;
  48.  
  49.         /// <summary>
  50.         /// Override OnGUI to show a maskable field.
  51.         /// </summary>
  52.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent content)
  53.         {
  54.             // We use a bunch of reflection tricks to try and ascertain the type of the passed-in SerializedProperty.
  55.             var propertyField = property.serializedObject.targetObject.GetType().GetField( property.name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance );
  56.             if ( propertyField != null )
  57.                 _objectType = propertyField.FieldType;
  58.  
  59.             // Help us out if we messed up
  60.             if ( property.propertyType != SerializedPropertyType.Enum )
  61.             {
  62.                 EditorGUI.HelpBox( position, "System.Flags is only valid on Enumeration types", MessageType.Error );
  63.             }
  64.             else if ( _objectType == null )
  65.             {
  66.                 EditorGUI.HelpBox( position, "Could not deduce Enumeration Type", MessageType.Error );
  67.             }
  68.             else
  69.             {
  70.                 // Just add-on some text so we know we're working...
  71.                 content.text += " (Flags)";
  72.                 var valueObj = System.Enum.ToObject( _objectType, property.intValue );
  73.                 var newValue = EditorGUI.EnumMaskField( position, content, valueObj as System.Enum );
  74.                 if ( GUI.changed )
  75.                 {
  76.                     property.intValue = newValue.GetHashCode();
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment