본문 바로가기
🌐 유니티 (Unity)

Day 65 - Unity Animator 효율적으로 관리 - Enum 활용 🎥

by shin0707 2025. 1. 8.
728x90
반응형
728x90

  • 주제

>>  Enum을 사용하는 이유

>>  Enum으로 Animator 상태 관리하기

>>  Enum과 스크립트 연동: 다중 Animator 제어

>>  Enum을 활용한 상태 전환 로직 확장

>>  이해를 돕는 다이어그램

>>  최적화 팁


  • 공부내용

Unity Animator의 상태 전환 시 **숫자 값(int)**을 직접 사용하는 대신 Enum을 활용하면 코드의 가독성과 유지보수성이 크게 향상된다.

이번 게시글에서는 Animator의 상태 전환을 Enum으로 관리하는 방법을 중심으로 설명하며,

다양한 예제와 팁을 소개한다.

 

1. Enum을 사용하는 이유

 

1-1. 숫자 값 사용의 단점

  • 숫자의 의미를 이해하기 어렵다. (1이 Move인지 Idle인지 기억하기 어렵다.)
  • 값이 변경되면 모든 관련 코드를 수정해야 한다.

1-2. Enum 사용의 장점

  • 명확한 의도를 코드에 표현할 수 있다.
  • 코드 수정 시 일관성이 유지된다.

 

2. Enum으로 Animator 상태 관리하기

 

2-1. Enum 정의

Animator 상태를 Enum으로 정의한다.

public enum PlayerActionType
{
    Idle = 0,
    Move = 1,
    Attack = 2,
    Special = 3
}

 

2-2. Animator 상태 전환 코드

이제 SetInteger 메서드에 Enum 값을 사용한다.

using UnityEngine;

public class PlayerAnimatorController : MonoBehaviour
{
    public Animator playerAnimator;

    public void SetPlayerAction(PlayerActionType actionType)
    {
        playerAnimator.SetInteger("ActionType", (int)actionType);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.W)) // 이동
        {
            SetPlayerAction(PlayerActionType.Move);
        }
        else if (Input.GetKeyDown(KeyCode.Space)) // 공격
        {
            SetPlayerAction(PlayerActionType.Attack);
        }
        else if (Input.GetKeyDown(KeyCode.Q)) // 특수 공격
        {
            SetPlayerAction(PlayerActionType.Special);
        }
        else if (Input.GetKeyDown(KeyCode.S)) // 기본 상태
        {
            SetPlayerAction(PlayerActionType.Idle);
        }
    }
}

 

3. Enum과 스크립트 연동: 다중 Animator 제어

 

3-1. 추가 Enum 상태

public enum EngineState
{
    Off = 0,
    Idle = 1,
    Boost = 2
}

public enum AttackState
{
    Inactive = 0,
    Active = 1,
    Special = 2
}

 

3-2. 스크립트 예제

using UnityEngine;

public class MultiAnimatorController : MonoBehaviour
{
    public Animator engineAnimator;
    public Animator attackAnimator;

    public void SetEngineState(EngineState state)
    {
        engineAnimator.SetInteger("EngineState", (int)state);
    }

    public void SetAttackState(AttackState state)
    {
        attackAnimator.SetInteger("AttackState", (int)state);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E)) // 엔진 부스트
        {
            SetEngineState(EngineState.Boost);
        }
        else if (Input.GetKeyDown(KeyCode.A)) // 공격 활성화
        {
            SetAttackState(AttackState.Active);
        }
        else if (Input.GetKeyDown(KeyCode.S)) // 엔진 꺼짐
        {
            SetEngineState(EngineState.Off);
        }
    }
}

 

4. Enum을 활용한 상태 전환 로직 확장

 

4-1.상태 이름과 설명을 문자열로 변환

public static class PlayerActionTypeExtensions
{
    public static string ToDescription(this PlayerActionType actionType)
    {
        return actionType switch
        {
            PlayerActionType.Idle => "Standing still",
            PlayerActionType.Move => "Moving forward",
            PlayerActionType.Attack => "Attacking the enemy",
            PlayerActionType.Special => "Performing a special move",
            _ => "Unknown action"
        };
    }
}

// 사용 예제
Debug.Log(PlayerActionType.Move.ToDescription()); // "Moving forward"

 

 

4-2. Animator의 현재 상태를 Enum으로 가져오기

public PlayerActionType GetCurrentActionType()
{
    int currentState = playerAnimator.GetInteger("ActionType");
    return (PlayerActionType)currentState;
}

// 사용 예제
Debug.Log("Current Action: " + GetCurrentActionType());

 

5. 이해를 돕는 다이어그램

Animator 상태 다이어그램

[Idle] (ActionType == Idle) --> [Move] (ActionType == Move)
[Move] --> [Attack] (ActionType == Attack)
[Attack] --> [Special] (ActionType == Special)
[Special] --> [Idle]

 

6. 최적화 팁

 

  • Enum으로 상태 관리 일원화
    Animator뿐 아니라 게임 전체에서 상태 관리를 Enum으로 통합하면 코드가 간결해진다.
  • 상태 전환 조건 최소화
    불필요한 연결을 줄이고 필요한 상태만 연결해 성능을 향상시킨다.
  • Blend Tree 활용
    움직임 속도나 방향이 다양한 경우 Blend Tree로 매끄럽게 처리한다.

<마무리>

Animator에서 숫자 값을 사용하는 대신 Enum으로 상태를 관리하면 코드의 명확성과 유지보수성이 크게 개선된다.

이러한 접근법은 단순한 애니메이션 제어를 넘어 프로젝트 전체의 상태 관리에도 적용할 수 있다.

다음 게시글에서는 Blend Tree를 활용한 애니메이션 전환의 고급 설정 방법을 소개할 예정이다.

 

728x90
반응형

loading