본문 바로가기
Unity3D/Unity & C#

Enum FlagsAttribute : enum을 비트 필드(플래그 집합)으로 처리하기

by 캬캬백곰 2022. 4. 6.
728x90

https://docs.microsoft.com/ko-kr/dotnet/api/system.flagsattribute?view=netframework-4.8

 

FlagsAttribute 클래스 (System)

열거형을 비트 필드 즉, 플래그 집합으로 처리할 수 있음을 나타냅니다.

docs.microsoft.com

 

유니티에서 enum에 복수의 값을 받아서 처리할 때 사용

[Flags]
public enum Type
{
	None = 0,
    Type0 = 1,
    Type1 = 2,
    Type2 = 4,
    All = int.MaxValue
}

Type typeValue = Type.None;

void Awake()
{
	SetType(Type.Type0 | Type.Type2);
    SetType(Type.Type1);
    SetType((Type)3);
}

void SetType(Type _type)
{
	typeValue = _type;
    
    Debug.Log("Type0 : " + typeValue.HasFlag(Type.Type0));
    Debug.Log("Type1 : " + typeValue.HasFlag(Type.Type1));
    Debug.Log("Type2 : " + typeValue.HasFlag(Type.Type2));
    Debug.Log("Type All : " + typeValue.HasFlag(Type.All));
}
728x90
반응형