How to use Enum Flags in Unity C#
Create sets of related flags and get rid of all the Boolean checks!
Introduction
As a developer most people are aware of enums. An Enum is a special type of class that is used to assign constant names to groups of numeric integers. Instead of booleans for “canMove”, “hasItem” etc, you can represent the state of an object in a more descriptive way.
However sometimes you may need to allow for more than one enum to be active at a time. This can be done using Enum Flags. Enum flags are a specific type of enum in C# that can be used to represent sets of related boolean values. In Unity, you can see this used in places such as Shader channels, blocking masks, and other internal properties. In this article, we will discuss how to use enum flags in Unity C#.
Creating an Enum Flag
Let’s get started with creating an Enum Flag. Then we will show an example in practice.
To define an enum flag, you start by defining an enum type using the enum keyword, like you would with a standard enum. The enum keyword is followed by the name of the enum type and the values that it can take. Each…