본문 바로가기
개발/Unity

유니티 C# - Switch Expression (스위치 표현식)

by 피로물든딸기 2022. 7. 8.
반응형

Unity 전체 링크

 

방향을 Enum으로 0, 1, 2, 3, 4, 5로 아래와 같이 정의하자.

    enum DIRECTION
    {
        FORWARD, BACK, RIGHT, LEFT, UP, DOWN,
    }

 

이렇게 하면 for 루프로 순회가 가능하다.

for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++) //Do Something

 

그런데 방향에 대한 실제 Vector가 필요하다고 가정하자. (FORWARD라면 Vector.forward)

그러면 아래의 함수가 필요하다.

    Vector3 getDirection(DIRECTION dir)
    {
        if (dir == DIRECTION.FORWARD) return Vector3.forward;
        if (dir == DIRECTION.BACK) return Vector3.back;
        if (dir == DIRECTION.RIGHT) return Vector3.right;
        if (dir == DIRECTION.LEFT) return Vector3.left;
        if (dir == DIRECTION.UP) return Vector3.up;
        if (dir == DIRECTION.DOWN) return Vector3.down;

        return Vector3.zero;
    }

 

또는 switch문으로 작성할 수 있다.

    Vector3 getDirectionSwitch(DIRECTION dir)
    {
        switch(dir)
        {
            case DIRECTION.FORWARD:
                return Vector3.forward;
            case DIRECTION.BACK:
                return Vector3.back;
            case DIRECTION.RIGHT:
                return Vector3.right;
            case DIRECTION.LEFT:
                return Vector3.left;
            case DIRECTION.UP:
                return Vector3.up;
            case DIRECTION.DOWN:
                return Vector3.down;
            default:
                return Vector3.zero;
        }
    }

 

여기서 C#의 문법인 Switch Expression을 사용하면 코드가 좀 더 보기 좋아진다.

    Vector3 getDirectionSwitchExpression(DIRECTION dir)
    {
        return dir switch
        {
            DIRECTION.FORWARD => Vector3.forward,
            DIRECTION.BACK => Vector3.back,
            DIRECTION.RIGHT => Vector3.right,
            DIRECTION.LEFT => Vector3.left,
            DIRECTION.UP => Vector3.up,
            DIRECTION.DOWN => Vector3.down,
            _ => Vector3.zero, /* default */
        };
    }

"_" 는 switch의 default 문을 의미한다.

 

그런데 이 코드는 return을 지우고 한 번 더 간단히 나타낼 수 있다.

    Vector3 getDirectionSwitchExpression2(DIRECTION dir) => dir switch
    {
        DIRECTION.FORWARD => Vector3.forward,
        DIRECTION.BACK => Vector3.back,
        DIRECTION.RIGHT => Vector3.right,
        DIRECTION.LEFT => Vector3.left,
        DIRECTION.UP => Vector3.up,
        DIRECTION.DOWN => Vector3.down,
        _ => Vector3.zero, /* default */
    };

 

따라서 아래의 코드는 모두 같은 결과를 나타낸다.

    void Start()
    {
        for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++)
            Debug.Log(getDirection(i));

        for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++)
            Debug.Log(getDirectionSwitch(i));

        for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++)
            Debug.Log(getDirectionSwitchExpression(i));

        for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++)
            Debug.Log(getDirectionSwitchExpression2(i));
    }

FORWARD와 BACK은 방향이 정 반대이다. 

이 경우에는 단순히 양의 방향인 Vector3.forward를 return하는 함수가 필요하다고 하자.

RIGHT, LEFT는 Vector3.right를 UP, DOWN은 Vector3.up을 리턴한다.

 

Switch Expression에서는 "or"을 이용해서 간단히 해결할 수 있다.

    Vector3 getDirectionSwitchExpression_OR(DIRECTION dir)
    {
        return dir switch
        {
            DIRECTION.FORWARD or DIRECTION.BACK => Vector3.forward,
            DIRECTION.RIGHT or DIRECTION.LEFT => Vector3.right,
            DIRECTION.UP or DIRECTION.DOWN => Vector3.up,
            _ => Vector3.zero, /* default */
        };
    }

 

이런 경우가 생길 때, Switch Expression을 한 번 이용해보자.

 

전체 코드는 아래와 같다.

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

public class SwitchExpression : MonoBehaviour
{
    enum DIRECTION
    {
        FORWARD, BACK, RIGHT, LEFT, UP, DOWN,
    }

    Vector3 getDirection(DIRECTION dir)
    {
        if (dir == DIRECTION.FORWARD) return Vector3.forward;
        if (dir == DIRECTION.BACK) return Vector3.back;
        if (dir == DIRECTION.RIGHT) return Vector3.right;
        if (dir == DIRECTION.LEFT) return Vector3.left;
        if (dir == DIRECTION.UP) return Vector3.up;
        if (dir == DIRECTION.DOWN) return Vector3.down;

        return Vector3.zero;
    }

    Vector3 getDirectionSwitch(DIRECTION dir)
    {
        switch (dir)
        {
            case DIRECTION.FORWARD:
                return Vector3.forward;
            case DIRECTION.BACK:
                return Vector3.back;
            case DIRECTION.RIGHT:
                return Vector3.right;
            case DIRECTION.LEFT:
                return Vector3.left;
            case DIRECTION.UP:
                return Vector3.up;
            case DIRECTION.DOWN:
                return Vector3.down;
            default:
                return Vector3.zero;
        }
    }

    Vector3 getDirectionSwitchExpression(DIRECTION dir)
    {
        return dir switch
        {
            DIRECTION.FORWARD => Vector3.forward,
            DIRECTION.BACK => Vector3.back,
            DIRECTION.RIGHT => Vector3.right,
            DIRECTION.LEFT => Vector3.left,
            DIRECTION.UP => Vector3.up,
            DIRECTION.DOWN => Vector3.down,
            _ => Vector3.zero, /* default */
        };
    }

    Vector3 getDirectionSwitchExpression2(DIRECTION dir) => dir switch
    {
        DIRECTION.FORWARD => Vector3.forward,
        DIRECTION.BACK => Vector3.back,
        DIRECTION.RIGHT => Vector3.right,
        DIRECTION.LEFT => Vector3.left,
        DIRECTION.UP => Vector3.up,
        DIRECTION.DOWN => Vector3.down,
        _ => Vector3.zero, /* default */
    };

    Vector3 getDirectionSwitchExpression_OR(DIRECTION dir)
    {
        return dir switch
        {
            DIRECTION.FORWARD or DIRECTION.BACK => Vector3.forward,
            DIRECTION.RIGHT or DIRECTION.LEFT => Vector3.right,
            DIRECTION.UP or DIRECTION.DOWN => Vector3.up,
            _ => Vector3.zero, /* default */
        };
    }

    void Start()
    {
        for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++)
            Debug.Log(getDirection(i));

        for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++)
            Debug.Log(getDirectionSwitch(i));

        for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++)
            Debug.Log(getDirectionSwitchExpression(i));

        for (DIRECTION i = DIRECTION.FORWARD; i < DIRECTION.DOWN; i++)
            Debug.Log(getDirectionSwitchExpression2(i));
    }
}

 

Unity Plus:

 

Easy 2D, 3D, VR, & AR software for cross-platform development of games and mobile apps. - Unity Store

Have a 2D, 3D, VR, or AR project that needs cross-platform functionality? We can help. Take a look at the easy-to-use Unity Plus real-time dev platform!

store.unity.com

 

Unity Pro:

 

Unity Pro

The complete solutions for professionals to create and operate.

unity.com

 

Unity 프리미엄 학습:

 

Unity Learn

Advance your Unity skills with live sessions and over 750 hours of on-demand learning content designed for creators at every skill level.

unity.com

반응형

댓글