Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Please make enum flags always use EnumFlagsField as default prperty drawer

Discussion in 'Immediate Mode GUI (IMGUI)' 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))
    rakkarage and BinaryCats like 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.         EditorGUI.BeginChangeCheck();
    7.         uint a = (uint)(EditorGUI.MaskField(position, label, property.intValue, property.enumNames));
    8.         if (EditorGUI.EndChangeCheck())
    9.         {
    10.             property.intValue = (int)a;
    11.         }
    12.     }
  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