본문 바로가기
개발/C#

유니티 C# 열거형 Enum 간단 사용법

by SPNK 2022. 6. 19.
반응형

Enum 이란?

상수에 이름을 붙여 구분을 쉽게 하기위해 사용합니다.

 

  • Enum을 사용하지 않고 코드 작성시
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExampleEnum : MonoBehaviour
{
    int gold = 0;
    int crystal = 1;

    int money = 0;

    void Awake()
    {
    	money = 0;
        
        switch (money)
        {
            case 0:
                Debug.Log("골드 발견");
                break;
            case 1:
                Debug.Log("크리스탈 발견");
                break;
            default:
                Debug.Log("아무것도 발견하지 못했습니다");
                break;
        }
    }
}

 

  • Enum을 사용해서 코드 작성시
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum MoneyType
{
    Gold = 0,
    Crystal
}

public class ExampleEnum : MonoBehaviour
{
    public MoneyType moneyType = MoneyType.Gold;


    void Awake()
    {
    	moneyType = MoneyType.Gold;
    
        switch (moneyType)
        {
            case MoneyType.Gold:
                Debug.Log("골드 발견");

                break;
            case MoneyType.Crystal:
                Debug.Log("크리스탈 발견");
                break;
            default:
                Debug.Log("아무것도 발견하지 못했습니다");
                break;
        }

        int number = (int)moneyType; //결과 : 0
    }
}

 


참고할만한 글

 

 

유니티 C# Enum Count 길이 간단 구하기

코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; public enum MoneyType { Gold = 0, Crystal } public class ExampleEnum : MonoBehaviour { public MoneyType moneyType = MoneyType.Gold; void GetEnumCount() { int count

parksh3641.tistory.com

 

 

유니티 C# String Enum 타입 간단 변환

코드 작성 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public enum MoneyType { Gold = 0, Crystal } public class ExampleEnum : MonoBehaviour { void Start() { MoneyType moneyType = (MoneyType)Enum.Parse(typ

parksh3641.tistory.com

 

반응형

댓글