Default Editor : Enum as flags ?

Hello there !

I juste have a little question regarding Unity default editor with enums. By default, when you declare an enum, it will create an editor when you can choose the enum value but you’ll only be able to pick one of those.

Is there a way like an special attribute to tell Unity “this is a flag, enable me to pick as many values as I want” without writing a custom editor ?

Thanks in advance for your answers :slight_smile:

This bugs me too but after some quick experimentation I think you can do it pretty trivially using the property drawer stuff. The main thing I noticed is to be aware of how Unity treats “Everything” as this sets all bits. I suspect it will also run in to problems if you leave gaps in your flags. I couldn’t find a way to get the actual System.Type from the SerializedProperty, if it were possible to get this then you could make it more robust and also decide whether you want everything to mean set all bits or not. Anyway the code:

public class EnumFlagsAttribute : PropertyAttribute
	public EnumFlagsAttribute() { }

public class EnumFlagsAttributeDrawer : PropertyDrawer
	public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label)
		_property.intValue = EditorGUI.MaskField( _position, _label, _property.intValue, _property.enumNames );

To use this you do the following:

public enum MyMaskedEnum
	Flag0 = (1 << 0),
	Flag1 = (1 << 1),
	Flag2 = (1 << 2),
	Flag3 = (1 << 3),

class MyObject : MonoBehaviour
    [SerializeField] [EnumFlagsAttribute] MyMaskedEnum m_flags;

I’ve also had this issue, and decided to build a different solution: Toggle Buttons.

alt text

It provides a better overview over the flags set but takes up more space in the Editor. You can check it out here:

As of writing this, just tag the enum definition as [System.Serializable, System.Flags] and unity will make the inspector ui work like a LayerMask. Makes everything on this page irrelevant. (using 2019.3 but i’m sure it has been there for a while)

(( Doh, sorry, I have to apologize. I thought it did what I described below, but I forgot that I had written a custom inspector just to handle this one case. Never mind! :slight_smile: ))

You just need to use [System.Flags]. Unity will pick this up and treat the enum drop-down as a mask.

For example, in one of my projects, I defined a bit flag enum like this:

public enum QuestStatus {
	Unassigned = 0x1,
	Active = 0x2,
	Success = 0x4,
	Failure = 0x8

Then I have a component similar to this:

public class Quest : MonoBehaviour {
    QuestStatus questStatus;

And the default inspector looks like the attached screenshot:

The answer posted by bunny83 solves this fully.

No need for custom editor scripts. The actual way to do this is to use the [Flags] attribute and the editor will do it automatically. The main two caveats are that you can’t have anything assigned to zero, as the editor will handle that with adding the ‘Nothing’ flag(just like with LayerMasks), and that each enum has to be assigned a value that is a power of two.

public enum AttackTypes
    Slash = 1,
    Point = 2,
    Blunt = 4

Then you can do bitwise operators on it to see stuff like if any of the flags match with a &, or if all the flags match(not sure the best way to do this as I rarely use these operations).

The official way (I think Unity does this for rigidbody constraints, and lots of traditional code uses hidden bitfields) is to set up the enum with all valid combinations, as bitfields. Suppose you want flags Airborne, Stunned and Dead:

enum statusFlags { none, Airborne=1, Stunned=2, Dead=4,
                   AirStun=3, AirDead=5};
// NOTE: Dead things can't be stunned

Then maybe something like:

bool hasStatus(statusFlags current, statusflags checkAgainst) {
  int s1 = (int)current;
  return s1&((int)checkAgainst) == s1;