Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

5 Commits

Repository files navigation

EnumFlags

Attribute Use

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.

Example Code:

public enum TestFlag
{
Test1 = 1,
Test2 = 2,
Test3 = 4,
Test4 = 8
}
[EnumFlags]
public TestFlag flag;

Inspector Display:

capture

public class EnumFlags<T>

Description:

Collection of static functions that allow for the simple implementation and checking of enumeration flags.

VisibilityReturn TypeMethod / FunctionDescription
public boolHasFlag(T valueToCheck ,T flagToCheck)Checks to see if the value to check is contained within the flag.
public boolHasAllFlags(T valueToCheck, params T[] flagsToCheck)Checks to see if the value to check contains all of the flags
privateboolCheckIfEnum()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
}

About

Enumeration Flags Unity Plugin

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors