๐ฎ Unity Input System - InputAction ์์ ์ ๋ณต
1. ๐งญ InputAction์ด๋?
InputAction์ Unity์ ์ Input System์์ ์ฌ์ฉ์์ ์ ๋ ฅ์ ์ถ์ํํ์ฌ "ํ๋" ๋จ์๋ก ์ ์ํ๋ ๊ตฌ์ฑ ์์์ ๋๋ค.
์๋ฅผ ๋ค์ด Jump, Move, Shoot ๊ฐ์ ๊ฒ์ ๋ด ํ๋(ํ์) ์ ์ ์ํ๊ณ , ์ฌ๊ธฐ์ ์ด๋ค ํค๋ณด๋/๋ง์ฐ์ค/๊ฒ์ํจ๋ ์ ๋ ฅ์ด ์ฐ๊ฒฐ๋ ์ง๋ฅผ ์ค์ ํฉ๋๋ค.
๐ฆ InputAction ๊ตฌ์ฑ ๊ตฌ์กฐ
InputActionAsset
โโ Action Map (์: Player)
โโ InputAction (์: Move, Jump)
โโ Binding (์: WASD, Spacebar, Gamepad A๋ฒํผ)
- InputActionAsset: ๋ชจ๋ ์ ๋ ฅ ํ๋์ ๋ด๋ ์ค์ ํ์ผ (๋ณดํต .inputactions ํ์ฅ์)
- Action Map: ๋ ผ๋ฆฌ์ ์ธ ์ ๋ ฅ ๊ทธ๋ฃน (์: Player, UI, Vehicle ๋ฑ)
- InputAction: ์ค์ ํ๋ ์ ์ (Move, Jump, Fire ๋ฑ)
- Binding: ํน์ ์ ๋ ฅ ์ฅ์น์์ ์ฐ๊ฒฐ (ํค๋ณด๋, ๋ง์ฐ์ค, ๊ฒ์ํจ๋ ๋ฑ)
โ๏ธ ๊ธฐ์กด ๋ฐฉ์ vs InputAction
๊ธฐ์กด ๋ฐฉ์ | InputAction |
Input.GetKeyDown(KeyCode.Space) | InputAction("Jump").performed += ... |
์ฝ๋ ๋ด์ ์ง์ ํค ์ ๋ ฅ ์ฒดํฌ | ํ๋ ๋จ์๋ก ์ถ์ํ๋ ๊ตฌ์กฐ |
์ ๋ ฅ๋ง๋ค ์กฐ๊ฑด๋ฌธ ํ์ | ์ ๋ ฅ ํ๋๋ณ๋ก ๋ถ๋ฆฌ ๊ฐ๋ฅ |
๋๋ฐ์ด์ค ๋ณ๊ฒฝ ์ ์ฝ๋ ์์ | ํ๋์ Action์ ์ฌ๋ฌ ๋๋ฐ์ด์ค ๋ฐ์ธ๋ฉ ๊ฐ๋ฅ |
๐ฌ ํ ๋ง๋๋ก ์ ๋ฆฌํ๋ฉด?
"InputAction์ '์ด๋ค ํค๋ฅผ ๋๋ ๋๊ฐ'๊ฐ ์๋๋ผ, '์ด๋ค ํ๋์ ์คํํ ๊ฒ์ธ๊ฐ'์ ์ง์คํ๋ ์์คํ ์ ๋๋ค."
์ด ๋ฐฉ์์ ๋ณต์กํ ์ ๋ ฅ ์ฒ๋ฆฌ, ๋ค์ํ ๋๋ฐ์ด์ค ๋์, ๊น๋ํ ์ฝ๋ ๊ตฌ์กฐ ๋ฑ์ ๋ชจ๋ ๋ง์กฑ์ํฌ ์ ์์ด ์ค๋ํ ํ๋ก์ ํธ์์ ํนํ ๊ฐ๋ ฅํ ์ ๋ ฅ ์ฒ๋ฆฌ ๊ตฌ์กฐ์ ๋๋ค.
2. ๐ฏ InputAction ํ์ ์ข ๋ฅ
InputAction์ ๋ง๋ค ๋ ์ ํํ ์ ์๋ Action Type์ ์ธ ๊ฐ์ง๊ฐ ์์ต๋๋ค:
โ Button (๋ฒํผ ์ ๋ ฅ)
- ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ ๋ ฅ ๋ฐฉ์์ ๋๋ค.
- ํค๋ณด๋๋ ๊ฒ์ํจ๋์ ํน์ ๋ฒํผ์ฒ๋ผ ๋๋ ๋ค ๋๋ค์ ์ด๋ฒคํธ๋ฅผ ๊ฐ์งํฉ๋๋ค.
- ์: ์ ํ, ๊ณต๊ฒฉ, ์ํธ์์ฉ ๋ฑ
InputAction jumpAction = new InputAction(type: InputActionType.Button);
jumpAction.performed += ctx => Jump();
- ๊ด๋ จ phase:
- started: ๋ฒํผ์ด ๋๋ฆฌ๋ ์๊ฐ
- performed: ๋์์ด ์ํ๋ ์๊ฐ
- canceled: ๋ฒํผ์์ ์์ ๋ ์๊ฐ
โก Value (๊ฐ ์ ๋ ฅ)
- ๋ฐฉํฅํค, ์คํฑ, ๋ง์ฐ์ค ์ด๋ ๋ฑ ์ฐ์์ ์ธ ๊ฐ์ ์ฒ๋ฆฌํ ๋ ์ฌ์ฉํฉ๋๋ค.
- Vector2, float, int ๋ฑ ๋ค์ํ ํ์ ์ ๊ฐ์ ๋ฐ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
- ์: ์ด๋, ๋ง์ฐ์ค ์์น, ์ค ๋ ๋ฒจ ๋ฑ
InputAction moveAction = new InputAction(type: InputActionType.Value);
Vector2 moveDir = moveAction.ReadValue<Vector2>();
โข Pass-through (์ฆ์ ์ ๋ฌํ)
- Unity ์ด๋ฒคํธ๋ ๋ค๋ฅธ ์ฒ๋ฆฌ ๋ก์ง์ ํตํ์ง ์๊ณ ๋ฐ๋ก ๊ฐ ์ ๋ฌ์ด ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉํฉ๋๋ค.
- performed๋ง ํธ์ถ๋๋ฉฐ started, canceled์ ํธ์ถ๋์ง ์์ต๋๋ค.
- ๋ณดํต ์ฒ๋ฆฌ ๊ณผ์ ์ ์๋ตํ๊ณ ์ ๋ ฅ ๊ฐ์ ๊ทธ๋๋ก ๋๊ฒจ์ผ ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
InputAction lookAction = new InputAction(type: InputActionType.PassThrough);
lookAction.performed += ctx => Look(ctx.ReadValue<Vector2>());
โ ํ์ ์ ํ ๊ธฐ์ค ์์ฝ
ํ์ | ์ฌ์ฉ ์์ | ํน์ง |
Button | ์ ํ, ๊ณต๊ฒฉ | ์ํ ๋ณํ๊ฐ ์๋ ๋จ์ ์ ๋ ฅ ์ฒ๋ฆฌ |
Value | ์ด๋, ์นด๋ฉ๋ผ, ์ค | ๊ฐ์ ๋ณํ๋์ ์ง์์ ์ผ๋ก ๊ฐ์ง |
Pass-through | ๋ง์ฐ์ค ์ด๋ | ๋น ๋ฅด๊ณ ์ง์ ์ ์ธ ๊ฐ ์ ๋ฌ, ํํฐ ์์ |
3. ๐ง InputAction ์ด๋ฒคํธ ์ฒ๋ฆฌ ์์ ์ดํด
InputAction์ ์ ๋ ฅ ์ด๋ฒคํธ๋ CallbackContext๋ฅผ ํตํด ๋ค์ํ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค.
๐ CallbackContext๋?
InputAction.CallbackContext๋ ์ ๋ ฅ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ ์ ๋ฌ๋๋ ๊ตฌ์กฐ์ฒด์ ๋๋ค. ์ด ์์๋ ์ด๋ค ๊ฐ์ด ๋๋ ธ๋์ง, ์ด๋ค ์ํ์ธ์ง ๋ฑ ์ค์ํ ์ ๋ณด๊ฐ ๋ด๊ฒจ ์์ต๋๋ค.
๐ ์ฃผ์ ์์ฑ ์ค๋ช
์์ฑ | ์ค๋ช |
ctx.phase | ์ ๋ ฅ์ ํ์ฌ ์ํ (Started, Performed, Canceled) |
ctx.ReadValue<T>() | ์ ๋ ฅ๊ฐ์ ์ํ๋ ํ์ ์ผ๋ก ๋ฐ์์ด (์: Vector2, float, bool) |
ctx.control.name | ์ด๋ค ์ ๋ ฅ ์ปจํธ๋กค์ด ๋๋ ธ๋์ง ํ์ธ ๊ฐ๋ฅ |
โ phase๋?
- Started: ํค๊ฐ ๋๋ฆฌ๊ธฐ ์์ํ์ ๋ ํธ์ถ
- Performed: ์ค์ ๋์์ด ์๋ฃ๋์์ ๋ ํธ์ถ (ex. ์คํ์ด์ค๋ฐ ๋๋ฌ์ ์ ํ)
- Canceled: ์ ๋ ฅ์ด ํด์ ๋์ ๋ ํธ์ถ (ex. ํค์์ ์ ๋)
public void OnJumpInput(InputAction.CallbackContext context)
{
if (context.phase == InputActionPhase.Started)
{
Debug.Log("์ ํ ์์!");
}
else if (context.phase == InputActionPhase.Performed)
{
Debug.Log("์ ํ ์คํ!");
}
else if (context.phase == InputActionPhase.Canceled)
{
Debug.Log("์ ํ ํด์ !");
}
}
โ ReadValue()๋?
์ ๋ ฅ์ผ๋ก๋ถํฐ ๊ฐ์ ๋ฐ์ ๋ ์ฌ์ฉ๋๋ ํจ์์ ๋๋ค.
- ReadValue<Vector2>() → ๋ฐฉํฅํค ์ ๋ ฅ
- ReadValue<float>() → ๋ง์ฐ์ค ํ , ํธ๋ฆฌ๊ฑฐ ์๋ ฅ
- ReadValue<bool>() → ๋จ์ ๋ฒํผ on/off
Vector2 movement = context.ReadValue<Vector2>();
โป ์ ๋ค๋ฆญ ํ์ ์ ์๋ชป ์ ๋ ฅํ๋ฉด ๋ฐํ์ ์๋ฌ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ ์ฃผ์ํด์ผ ํฉ๋๋ค.
4. ๐งฉ PlayerInput ์ปดํฌ๋ํธ์ InputAction ์ฐ๋
InputAction์ ์ฌ์ฉํ๋ ค๋ฉด Unity ์ค๋ธ์ ํธ์ ์ค์ ์ ๋ ฅ์ ๋ฐ์ ์๋จ์ด ํ์ํฉ๋๋ค. ๋ฐ๋ก ๊ทธ๊ฒ์ด PlayerInput ์ปดํฌ๋ํธ์ ๋๋ค.
PlayerInput์ InputActionAsset๊ณผ ์ฐ๋๋์ด ์ ๋ ฅ์ ๋ฐ์ ๋์์ ์คํํ๋๋ก ํด์ฃผ๋ ์ค๊ณ์ ์ญํ ์ ํฉ๋๋ค.
โ PlayerInput์ ๊ตฌ์ฑ ์์
ํญ๋ชฉ | ์ค๋ช |
Actions | ์ฌ์ฉํ InputActionAsset ํ์ผ ์ง์ |
Default Map | ์์ ์ ํ์ฑํํ Action Map ์ ํ (์: "Player") |
Behavior | ์ด๋ฒคํธ ์ฒ๋ฆฌ ๋ฐฉ์ ์ ํ (SendMessage, UnityEvents, Invoke C# Events ๋ฑ) |
Camera | Look ์ ๋ ฅ์ ์นด๋ฉ๋ผ ๊ธฐ์ค์ผ๋ก ๋ณด์ ํ ๊ฒฝ์ฐ ์ฐ๊ฒฐ |
โ๏ธ Behavior ๋ฐฉ์ ๋น๊ต
โ SendMessage ๋ฐฉ์
- Unity์ ๊ธฐ๋ณธ ๋ฉ์์ง ์์คํ ํ์ฉ
- ์ก์ ์ด๋ฆ๊ณผ ๊ฐ์ ์ด๋ฆ์ ํจ์๋ฅผ MonoBehaviour์์ ์๋์ผ๋ก ์ฐพ์ ํธ์ถ
void OnJump(InputValue value) { ... }
- โ ๋น ๋ฅธ ๊ตฌํ ๊ฐ๋ฅ / โ ํผํฌ๋จผ์ค ์ด์ ๊ฐ๋ฅ, ํจ์๋ช ์ผ์น ํ์
โก UnityEvents ๋ฐฉ์
- ์ธ์คํํฐ์์ ์ก์ ๋ณ๋ก ํจ์ ์ฐ๊ฒฐ ๊ฐ๋ฅ
- ๋น๊ฐ๋ฐ์๋ ์ค์ ๊ฐ๋ฅํ์ฌ ํ์ ์ ์ ๋ฆฌ
- PlayerInput → Events ํญ์์ ์ง์ ํจ์ ์ฐ๊ฒฐ
// PlayerController.cs
public void OnMove(InputAction.CallbackContext context) { ... }
โข C# Events ๋ฐฉ์
- ๊ฐ์ฅ ์ ์ฐํ๊ณ ํผํฌ๋จผ์ค๊ฐ ์ข์ ๋ฐฉ์
- ์ฝ๋์์ ์ง์ InputAction์ ์ ๊ทผํ๊ณ .performed += ์ผ๋ก ์ฐ๊ฒฐ
playerInput.actions["Jump"].performed += ctx => Jump();
- โ ์ฝ๋ ๊ธฐ๋ฐ์ผ๋ก ์ฒ ์ ํ ์ ์ด ๊ฐ๋ฅ / โ ์ด๋ณด์์๊ฒ ์ง์ ์ฅ๋ฒฝ ์์
๐ฎ ์ค์ต ์์: UnityEvents ๋ฐฉ์ ์ฐ๊ฒฐ
- Player ์ค๋ธ์ ํธ์ PlayerInput ์ปดํฌ๋ํธ ์ถ๊ฐ
- Actions์ .inputactions ํ์ผ ์ฐ๊ฒฐ
- Behavior๋ฅผ Invoke Unity Events๋ก ์ค์
- PlayerController ์คํฌ๋ฆฝํธ์ ์ด๋ฒคํธ ํจ์ ์์ฑ
- ์ธ์คํํฐ์์ ํด๋น ์ก์ ์ ํจ์ ์ฐ๊ฒฐ
public void OnMove(InputAction.CallbackContext context)
{
Vector2 input = context.ReadValue<Vector2>();
// ์ด๋ ์ฒ๋ฆฌ
}
๐ ํ: PlayerInput ์๋ ์์ฑ ๋ฐฉ์งํ๊ธฐ
PlayerInput ์ปดํฌ๋ํธ๋ฅผ ์๋์ผ๋ก ๋ถ์ฌ ์ฐ๋ ๊ฒฝ์ฐ, ์ฝ๋๋ ๋ค๋ฅธ ๋งค๋์ ์์ InputAction์ ์์ฑํ๋ฉด ์ค๋ณต ์์ฑ๋ ์ ์์ต๋๋ค. ํญ์ ํ๋์ PlayerInput๋ง ์ฌ์ฉํ๋๋ก ์ฃผ์.
'Unity' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Unity - Inventory ์ธ๋ฒคํ ๋ฆฌ ์์คํ ๊ตฌํ ์ ๋ฆฌ (3) | 2025.08.07 |
---|---|
Unity - Unity์ TryGetComponent (1) | 2025.08.07 |
ํ๋ก์ ํธ ์๊ฐ - ใํ๋์ธ๊ณ ์ตํ์ ๊ถ์ใ (1) | 2025.08.05 |
Unity - Unity์ ๋ฉ์ธ ์นด๋ฉ๋ผ(Main Camera) & Projection (2) | 2025.08.04 |
Unity - Unity Instantiate() ์ค๋ฒ๋ก๋ ์ ๋ฆฌ & ์์์ผ๋ก ์ค์ ํ ๋์ ์ฐจ์ด (0) | 2025.07.28 |