Unity 기본9 - enum, struct

1. enum

 

저장되는 데이터에 이름을 붙여서 읽기 편한 코드를 만드는 것

 

내부적으로는 int로 정수형과 같다

 

0,1,2,... 로 되는데.. 

 

arrow - 0

bullet - 1

missile - 2

...

 

파이썬의 enumerate 같은 거인듯?

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{

    enum ProjectileKind
    {
        Arrow,
        Bullet,
        Missile
    }
    // Start is called before the first frame update
    void Start()
    {
        ProjectileKind kind;
        kind = ProjectileKind.Arrow;

    }

    // Update is called once per frame
    void Update()
    {

    }
}

 

 

 

ProjectileKind라는 이름의 enum 데이터 타입을 하나 만든건데

 

kind라는 변수에 "화살"이라는 뜻을 가지게 하고 싶어서

 

kind = 0;으로 저장하는 것보다

 

kind = ProjectileKind.Arrow;로 저장해두는게 보기 좋다

 

kind = 0이라고 하면 나중에 볼때 이게 화살인지 총알인지 뭔지 알수 없어서

 

Debug.Log(kind);하면 enum타입에서 지정된 글자 Arrow라고 나온다

 

 

 

내부적으로는 enum은 int와 같아서, kind = 0;이라고 하고 출력해보면 Arrow라고 나온다

 

 

 

 

 

타입 캐스팅이라고 타입 변환이 가능한데, 내부적으로는 int로 저장되어 있다보니...

 

실제로 (int)kind로 해서 int로 바꿀 수 있다

 

이런 타입캐스팅은 실제로 가능할 경우에만, 바꿀 수 있는데 (int)로 못바꾸는 경우라면 에러가 난다

 

Debug.Log((int)kind);

 

 

 

ProjectileKind도 하나의 데이터타입이라서, 타입캐스팅이 가능한데

 

0을 ProjectileKind로 바꾼다면

 

Debug.Log((ProjectileKind)0);

 

 

 

 

int와 enum은 상호변환이 가능한데, 굳이 enum을 쓰는 이유는.. 숫자 0,1,2,...라고 하는 것보다

 

각 숫자의 의미가 arrow, bullet, missile,...라고 있기때문에, 실제로 이렇게 쓰면 보기 좋으니까 

 

 

enum은 switch 문을 이용해서 저장된 값을 비교해가지고, 처리하는데 활용할 수 있다

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{

    enum ProjectileKind
    {
        Arrow,
        Bullet,
        Missile
    }
    // Start is called before the first frame update
    void Start()
    {
        ProjectileKind kind;
        kind = ProjectileKind.Arrow;

        switch (kind)
        {
            case ProjectileKind.Arrow:
                Debug.Log("화살입니다.");
                break;
            case ProjectileKind.Bullet:
                Debug.Log("총알입니다.");
                break;
            case ProjectileKind.Missile:
                Debug.Log("미사일입니다.");
                break;
        }

    }

    // Update is called once per frame
    void Update()
    {

    }
}

 

 

enum으로 Arrow, Bullet, Missile,...가 순서대로 0,1,2...로 된다는 것인데

 

다음과 같이 내가 원하는 숫자로 지정할 수도 있긴 있다

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{

    enum ProjectileKind
    {
        Arrow = 2,
        Bullet = 1,
        Missile = 3
    }
    // Start is called before the first frame update
    void Start()
    {

        Debug.Log((ProjectileKind)3);

    }

    // Update is called once per frame
    void Update()
    {

    }
}

 

 

 

 

 

2. struct

 

여러 자료형들을 한데 모아놓은것

 

예를 들어 어떤 사람에게는 이름, 체중, 키 등등의 정보가 있는데 이들을 한데 모아놓은 것이다

 

struct 이름

{

속성

} 으로 선언하고

 

변수 선언하듯이 (struct이름) 이름 = new (struct이름)();

 

이렇게 만든 구조체에 속성을 집어넣을려면

 

이름.속성1 = 값;이름.속성2 = 값;... 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{

    struct HumanData
    {
        public string name;
        public float weight;
        public float height;
        public float feetSize;
    }
    // Start is called before the first frame update
    void Start()
    {

        HumanData Daehyuck = new HumanData();
        Daehyuck.name = "대혁";
        Daehyuck.weight = 60;
        Daehyuck.height = 175;
        Daehyuck.feetSize = 260;

        Debug.Log(Daehyuck);
    }

    // Update is called once per frame
    void Update()
    {

    }
}

 

 

구조체 그대로 출력하면... 안에 내용을 알려주지는 않는다

 

 

 

 

해당 구조체를 여러개 저장하는 배열도 만들 수 있다

 

구조체 타입의 배열을 선언해서

 

HumanData[] players = new HumanData[3];
players[0] = new HumanData();
players[1] = new HumanData();
players[2] = new HumanData();

 

TAGS.

Comments