public enum ETreeHolderType
{
NULL,
TutorialBoss,
FlameWizard,
NightSword,
NightSwordCopy,
Elf,
ElfTree,
Ninja,
NinjaCopy,
IceWizard,
SwordMaster,
Granny,
Obelisk,
END,
}
이런 식으로 behavior tree가지고있는 오브젝트들을 enum값을 이용해 관리중이였다.
여기서 string형으로 저걸 접근할 수 있나 해서 찾아봤다.
예를들어 string 변수로 "TutorialBoss"를 할당하고 이거에 해당하는 enum값을 가져오는 방식이다.
Enum.TryParse(TreeHolderTypeStr,out ETreeHolderType tmpEnemyType);
이런식으로 TryParse함수를 이용해 string값에서 뽑아올 수 있었다!
string typeName = Enum.GetName(typeof(ETreeHolderType),treeHolderType);
이런 식으로 string값을 받아올 수 있었다.
어떤 dictionary는 string형을 키로 이용해서 관리하기때문에
enum값을 string으로 변경해줘야해서
위 코드를 이용해서 GetTreeHolderTypeName()을 추가해서
string값으로 불러오도록 구현했다.
public struct BehaviorTreeHolder
{
public ETreeHolderType treeHolderType;
public List<BehaviourTree> behaviorTreeList;
public List<float> phaseHPList;
public string GetTreeHolderTypeName()
{
string typeName = Enum.GetName(typeof(ETreeHolderType),treeHolderType);
if(string.IsNullOrEmpty(typeName) )
{
Debug.Assert(false);
Debug.LogError("treeHolderType의 type이 null값입니다.");
return null;
}
return typeName;
}
추가) 더 간단한 방법은 enum값에 .ToString()메소드를 통해 해당 enum형 value를 문자열로 받아올수가 있었다.
문제는 위 방법이나 이 추가한 방법이나 리플렉션 데이터를 접근하기때문에 비싼연산이라고 한다.
이번 경우는 자주 호출되는것도 아니고 초기화하는 부분에서만 사용하기 때문에 상관없지만
그냥 enum값에 대응하는 string값 저장하는 dictionary하나 구현해서 쓰는게 나을것 같다..