The use of this attribute bypasses the need to use [System.Flags] attribute during the declaration of the Enumeration as long as the values within the Enumerations are set up correctly with 2^nth power. It also changes the Enumeration drop down within the inspector and allows for multiple selections to be chosen at the same time.
public enum TestFlag
{
Test1 = 1,
Test2 = 2,
Test3 = 4,
Test4 = 8
}
[EnumFlags]
public TestFlag flag;
Description:
Collection of static functions that allow for the simple implementation and checking of enumeration flags.
Visibility | Return Type | Method / Function | Description |
---|---|---|---|
public | bool | HasFlag(T valueToCheck ,T flagToCheck) | Checks to see if the value to check is contained within the flag. |
public | bool | HasAllFlags(T valueToCheck, params T[] flagsToCheck) | Checks to see if the value to check contains all of the flags |
private | bool | CheckIfEnum() | Checks the current type T is an Enumeration. |
The use of flags within Unity can be done through the use of the [EnumFlags] Attribute found here.
//ExampleCode
public enum TestFlagEnum
{
Test1 = 1,
Test2 = 2,
Test3 = 4
}
[EnumFlags]
[SerializedField]
private TestFlagEnum flagToCheck = TestFlagEnum.Test1 | TestFlagEnum.Test2 | TestFlagEnum.Test3;
void Start()
{
Debug.Log("Does flagToCheck contain the Flag? " +
FlagsEnum<TestFlagEnum>.HasFlag(TestFlagEnum.Test1,flagToCheck));
//Will return 'Does flagToCheck contain the Flag? true
TestFlagEnum test2 = TestFlagEnum.test2;
Debug.Log("Does flagToCheck contain the Flag? " +
FlagsEnum<TestFlagEnum>.HasFlag(test2, flagToCheck));
//Will return 'Does flagToCheck contain the Flag? true
}