Unity / C#
Beginner
Unity 2021.3+ / Unity 6
신규 Unity Input System (Action Map) 이벤트 바인딩 및 크로스플랫폼 통합 제어
레거시 Input.GetAxis 폴링을 탈피하여 키보드, 마우스, 게임패드, 터치스크린 입력을 단일 InputAction 에셋으로 일원화하는 실무 C# 이벤트 처리 아키텍처.
#Unity #C# #InputSystem #CrossPlatform #Architecture
핵심 개념하드코딩된
Input.GetKey(KeyCode.Space) 대신, 추상화된 액션(예: Move, Jump, Interact)을 정의하고 이벤트 콜백(started, performed, canceled)으로 수신하면 패드 진동, 키 리바인딩, 모바일 가상 패드 대응이 한 번에 해결됩니다.1. C# C# 클래스 자동생성 바인딩 코드
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!");
}
}