Enum flags in Unity3d Inspector

It’s quite strange but Unity can’t show your enum as bitmask / flags out of the box.
Fortunately this can be fixed fast and easy.

Step1. Attribute

First of all we need an attribute which should inform Unity that we want it to show our enum as flags and not as usual one-option list of enums. That’s pretty simple and straightforward.

Step2. PropertyDrawer

Next part is a custom PropertyDrawer. Its purpose is to draw a property with which EnumFlagAttribute was associated.

Nothing special as well. Maybe except the 11-th line where we receive the value of enum from SerializedProperty. Unity’s documentation lacks detailed information about PropertyDrawer.fieldInfo but that’s one of the ways how to use it.

Step3. Usage

Usage is easy peasy. Just associate EnumFlag attribute with an enum field and enjoy your bitmask in the Inspector.

Share Comments
comments powered by Disqus