How do I customize a EnumMaskDrawer?

I want custom a enum mask property using PropertyDrawer, I can not find EnumValue from SerializedProperty class, so I use property.enumValueIndex with EditorGUILayout.MaskField, but when I set back the value to property.enumValueIndex, it raise a error:

enum index is out of range
UnityEditor.SerializedProperty:set_enumValueIndex(Int32)

    [CustomPropertyDrawer(typeof(EnumMaskAttribute))]
    internal sealed class EnumMaskDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EnumMaskAttribute attribute = (EnumMaskAttribute)base.attribute;
            if (property.propertyType == SerializedPropertyType.Enum)
            {
                property.enumValueIndex = EditorGUILayout.MaskField(label, property.enumValueIndex, property.enumNames);
            }
            else
            {
                EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
            }
        }
    }

Is any body has this problem, and how to solve it, or how do I custom the Enum mask property?

Your code is in the right direction, here are a few issues i found:

  1. You are retrieving the EnumMaskAttribute (line 6 in your code) but not doing anything with it.

  2. You are using EditorGUILayout in the property drawer, you should use the corresponding EditorGUI method and pass the Rect that you receive in the OnGUI method.

  3. The enumValueIndex may be negative and may not correspond to actual values you have in your enum field. The enum mask add a few options (nothing, everything), and i guess these may have a negative index (for example, i got -1 for the index since i didn’t select anything). This doesn’t match any of the values i have in my enum type…