[Unity] Input System(2)

muchan
muchan
∙∙조회 9
[Unity] Input System(2)

연습해보기

우선 Package Manager에서 Input System을 검색하고 Samples에 들어간다

Simple Demo를 Import한다

그럼 화면이 보라색으로 보이는데 Window - Rendering - Renderer Pipeline Converter 들어가서
마지막거 체크하고 Initialize Converter -> Convert Assets 누르면 됨

그러면 Player오브젝트에 스크립트와 Player Input 컴포넌트가 붙어있다

Player Input

New Input System 패키지의 핵심 컴포넌트로 플레이어의 입력을 게임 오브젝트에 쉽게 연결해준다

현재 SimpleControls라는 Input Action Asset이 있고
그 안에 gameplay라는 Action Map이 있으며

거기서 fire 액션에는 오른쪽에 보이는 컨트롤과 바인딩 되어있다.

이벤트들을 연결해 Device가 Lost, Regained, Changed 됐을 때 이벤트가 발생되도록 할 수 있다.

이벤트 등록하기

csharp
public void Awake()
    {
        // We could use `fireAction.triggered` in Update() but that makes it more difficult to
        // implement the charging mechanism. So instead we use the `started`, `performed`, and
        // `canceled` callbacks to run the firing logic right from within the action.

        fireAction.performed +=
            ctx =>
        {
            if (ctx.interaction is SlowTapInteraction)
            {
                StartCoroutine(BurstFire((int)(ctx.duration * burstSpeed)));
            }
            else
            {
                Fire();
            }
            m_Charging = false;
        };
        fireAction.started +=
            ctx =>
        {
            if (ctx.interaction is SlowTapInteraction)
                m_Charging = true;
        };
        fireAction.canceled +=
            ctx =>
        {
            m_Charging = false;
        };
    }

Update에서 fireAction.triggered를 계속 검사할 수 있지만 차징 공격 같은 기능 구현이 복잡해질 수 있다.

그래서 이벤트 기반 콜백인

  • started
  • performed
  • canceled

를 사용해서 입력 상태를 직접 처리한다

performed

performed는 입력이 실제로 수행되었을 때 호출된다

ctx는 InputAction.CallbackContext로 현재 입력 상태 정보를 담고 있는 객체다

SlowTab이라는 판정이 나왔으면 저 코루틴(BurstFire)을 시작하게 된다


기본 InputSystem_Actions에서 필요한 입력들을 추가할 수 있다

미니 게임 마다 입력 키가 다르면 커스텀 input을 받을 수 있다