반응형
방향을 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:
Unity Pro:
Unity 프리미엄 학습:
반응형
'개발 > Unity' 카테고리의 다른 글
유니티 Attribute - DisallowMultipleComponent로 스크립트 중복 금지하기 (0) | 2022.07.09 |
---|---|
유니티 에셋 - Lean Touch로 마우스 클릭 이펙트 보여주기 (Click Effect / Input Management) (0) | 2022.07.09 |
유니티 - 코루틴으로 오브젝트 90도 회전하기 (Rotate GameObject using Coroutine) (0) | 2022.07.08 |
유니티 UI - Scale With Screen Size로 캔버스 UI 크기 자동 변환하기 (0) | 2022.07.04 |
유니티 UI - Text Mesh Pro 텍스트에 그림자 효과 넣기 (Drop Shadow) (0) | 2022.07.04 |
댓글