Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Please make enum flags always use EnumFlagsField as default prperty drawer

Discussion in 'Extensions & OnGUI' started by Thaina, May 6, 2018.

  1. Thaina


    Jul 13, 2012
    I don't know why you need us to create CustomPropertyDrawer just for having enum flag use EnumFlagsField

    Any enum that has FlagsAttribute should just use EnumFlagsField as default PropertyDrawer everywhere

    It could be checked easily with this code

    Code (CSharp):
    1. fieldInfo.FieldType.CustomAttributes.Any((attribute) => attribute.AttributeType == typeof(FlagsAttribute))
    BinaryCats likes this.
  2. BinaryCats


    Feb 8, 2016
    I guess the reason this may be difficult for them to do, is that an enum (to my knowledge) with the [Flags] attribute, still increases each value by 1

    Code (csharp):
    2. [Flags]
    3. enum MyFlags
    4. {
    5. a, //0
    6. b, //1
    7. c, //2
    8. d, //3
    9. }
    and for flags to work properly they would need to be as followed
    Code (csharp):
    2. enum MyFlags
    3. {
    4. a = 1, //1
    5. b =2 , //2
    6. c = 4, //4
    7. e = 8, //8
    8. }
    But I too find it annoying there is no default way to draw a lot of things which unity has methods for drawing, flags being one of them. It annoys me more that the EditorGUI.MaskField shows 'mixed' when two values are selected, rather than how it works on say LayerMasks showing up to 3 selected values before it goes mixed.

    just fyi, this is how we draw the mask field for anybody that is curious

    Code (csharp):
    2.     public override void OnGUI(Rect position,
    3.                                 SerializedProperty property,
    4.                                 GUIContent label)
    5.     {
    6.         uint a = (uint)(EditorGUI.MaskField(position, label, property.intValue, property.enumNames));
    7.         if (EditorGUI.EndChangeCheck())
    8.         {
    9.             property.intValue = (int)a;
    10.         }
    11.     }
  3. Thaina


    Jul 13, 2012
    @BinaryCats Not really, flag could be define as 0,1,2,3 if the person who create it know what they really want to do

    In fact the person who create enum with [flags] would really have knowledge about how to define enum value. So it's their responsible to define enum value properly

    It just that unity should be responsible for drawing all enum with [flags] by EnumFlagsField (and normal enum by EnumPopup) by default