ABOUT PORTFOLIO GAMES DEVLOG DOCS TOOLS CONTACT
HOME > DOCS > Unity / C# > 신규 Unity Input System (Action Map) 이벤트 바인딩 및 크로스플랫폼 통합 제어Unity Input System Action Map Binding and Cross-Platform Input Control
Unity / C# Beginner Unity 2021.3+ / Unity 6

신규 Unity Input System (Action Map) 이벤트 바인딩 및 크로스플랫폼 통합 제어Unity Input System Action Map Binding and Cross-Platform Input Control

레거시 Input.GetAxis 폴링을 탈피하여 키보드, 마우스, 게임패드, 터치스크린 입력을 단일 InputAction 에셋으로 일원화하는 실무 C# 이벤트 처리 아키텍처.A C# event-driven input architecture that replaces legacy Input.GetAxis polling and unifies keyboard, mouse, gamepad, and touchscreen inputs through a single InputAction asset.

#Unity #C# #InputSystem #CrossPlatform #Architecture
핵심 개념하드코딩된 Input.GetKey(KeyCode.Space) 대신, 추상화된 액션(예: Move, Jump, Interact)을 정의하고 이벤트 콜백(started, performed, canceled)으로 수신하면 패드 진동, 키 리바인딩, 모바일 가상 패드 대응을 일관되게 처리할 수 있습니다.
Core ConceptInstead of hardcoding Input.GetKey(KeyCode.Space), defining abstract actions (such as Move, Jump, and Interact) and receiving event callbacks (started, performed, canceled) lets you handle gamepad rumble, key rebinding, and mobile virtual controls consistently.

1. C# 클래스 자동 생성 바인딩 코드1. Auto-Generated C# Class Binding Code

PlayerInputController.cs
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerInputController : MonoBehaviour
{
    private PlayerInputActions _actions;
    public Vector2 MoveInput { get; private set; }

    private void Awake()
    {
        _actions = new PlayerInputActions();
    }

    private void OnEnable()
    {
        _actions.Player.Enable();
        _actions.Player.Move.performed += OnMove;
        _actions.Player.Move.canceled += OnMove;
        _actions.Player.Jump.performed += OnJump;
    }

    private void OnDisable()
    {
        _actions.Player.Move.performed -= OnMove;
        _actions.Player.Move.canceled -= OnMove;
        _actions.Player.Jump.performed -= OnJump;
        _actions.Player.Disable();
    }

    private void OnMove(InputAction.CallbackContext ctx)
    {
        MoveInput = ctx.ReadValue<Vector2>();
    }

    private void OnJump(InputAction.CallbackContext ctx)
    {
        Debug.Log("Jump Action Triggered");
    }
}

2. 실무 적용 및 주의사항2. Practical Considerations

  • 이벤트 구독 해제 및 비활성화: C# 래퍼 클래스의 액션에 등록한 콜백은 OnDisable()에서 반드시 -=로 해제하고 Disable()을 호출해야 씬 전환 시 메모리 누수와 파괴된 오브젝트 참조 오류를 방지할 수 있습니다.
  • Action Map 전환 관리: 인게임 플레이 도중 일시정지 메뉴나 인벤토리 창이 열릴 때는 Player 액션 맵을 비활성화하고 UI 액션 맵만 활성화하여 UI 조작 중 캐릭터가 동시에 움직이는 현상을 차단합니다.
  • 단발성 입력과 연속 입력의 분리: 점프나 공격 같은 단발성 동작은 performed 이벤트에서 즉시 처리하고, 이동처럼 매 프레임 적용되는 입력은 콜백에서 읽은 Vector2 값을 캐싱해 두었다가 Update나 FixedUpdate에서 참조하는 방식이 안정적입니다.
  • Unsubscribe and Disable Actions: Always unsubscribe callbacks using -= and call Disable() inside OnDisable() to prevent memory leaks and calls to destroyed objects across scene transitions.
  • Manage Action Map Switching: When opening a pause menu or inventory during gameplay, disable the Player action map and enable only the UI action map so character movement does not occur while navigating menus.
  • Separate Discrete and Continuous Inputs: Handle discrete actions like jumping or interacting directly inside the performed callback, while caching continuous Vector2 movement values to read inside Update or FixedUpdate.

Input Action 에셋과 C# 자동 생성 클래스를 활용하면 입력 장치별 분기 코드를 작성하지 않고도 키 리바인딩과 멀티플랫폼 입력을 체계적으로 관리할 수 있습니다.

Using Input Action assets with auto-generated C# wrapper classes allows you to manage key rebinding and multi-platform input without writing device-specific branching logic.

참고자료 & 관련 링크