
New Input System의 핵심 구성요소 (Action / Map / Binding)if (Input.GetKey(KeyCode.W)) { ..Behavior.. }
Input Action Asset
- Action Map
- Action (Move)
- Type: Value
Binding: 키보드, 마우스, 등등...
- Action (Jump)
- Type: Button
- Binding: 키보드, 마우스, 등등....
실제 입력 장치
별일 없으면 기본 컨트롤러 사용을 권장한다.
테스트를 위해 위와 같이 추가 후 컨트롤러에 장착하였다.

사용을 위해서는 Inspector에 Player Input Component 를 붙이고 정의한 Input System 을 불러오면 된다.
기본적으로 설정한 Input 의 Action 이름에
On을 붙임
ex: action 이 Jump 일 경우OnJump

// Parent.cs
public class Parent : MonoBehaviour
{
private void OnMove()
{
Debug.Log("<color=yellow>Parent Move</color>");
}
private void OnJump()
{
Debug.Log("<color=yellow>Parent Jump</color>");
}
}
// Child.cs
public class Child : MonoBehaviour
{
private void OnMove()
{
Debug.Log("<color=yellow>Child</color> Called Move!!");
}
private void OnJump()
{
Debug.Log("<color=red>Child</color> Called Jump!!");
}
}


using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMassegeController : MonoBehaviour
{
private Vector2 moveInput;
private void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
Debug.Log($"<color=yellow>Move: {moveInput}</color> ");
}
private void OnJump()
{
Debug.Log("<color=red>Jump</color> ");
}
}


⚠️주의: Binding 할 함수는 public 으로 해야한다.
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerEventController : MonoBehaviour
{
private Vector3 moveDir;
void Update()
{
transform.Translate(moveDir * (0.5f * Time.deltaTime));
}
public void OnMove(InputAction.CallbackContext context)
{
Vector2 move = context.ReadValue<Vector2>();
Debug.Log($"<color=red>Move</color> {move}");
moveDir = new Vector3(move.x, 0, move.y);
}
public void OnJump(InputAction.CallbackContext context)
{
if (context.performed)
{
Debug.Log("<color=red>Jump</color> ");
}
}
}
Move 시

콘솔로는 보기 어렵지만, 일부 Capsule 들이 하찮게 넘어져버렸다....😅
Jump 시

Jump Action에서 Button 형으로 설정했기 때문에 OnMove 에서 잘못된 변수 타입을 받게 되어 에러가 발생한 것이다.

using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerCsharpController : MonoBehaviour
{
PlayerInput playerInput;
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
}
private void OnEnable()
{
playerInput.onActionTriggered += HandleInput;
}
private void OnDisable()
{
playerInput.onActionTriggered -= HandleInput;
}
private void HandleInput(InputAction.CallbackContext ctx)
{
switch (ctx.action.name)
{
case "Move":
Vector2 move = ctx.ReadValue<Vector2>();
Debug.Log($"<color=green>Move</color> {move}");
break;
case "Jump":
if(ctx.performed) Debug.Log($"<color=yellow>Jump</color>");
break;
}
}
}
