Unity 게임 만들기 프로젝트 - InputManager 구현
반응형
1. InputManager 구성
각 script의 update 마다 Input 을 입력받도록 구성하게 되면 큰 게임을 구현할 때 관리가 어렵게 된다.
Input 을 관리하는 매니저를 구성하고, 이벤트를 구독하도록 구성해서 Input 을 관리할 수 있도록 구성한다.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputManager
{
public Action KeyAction = null;
public void OnUpdate()
{
if (Input.anyKey == false)
return;
if (KeyAction != null)
KeyAction.Invoke();
}
}
InputManager.cs 파일, Monobehavior 를 상속 받지 않았기 때문에 Unity Component 코드는 아니다.
따라서 start, update 등의 함수가 구현될 필요는 없음. Action 형식의 KeyAction을 선언해주고
OnUpdate 함수에 Input 값이 없다면 false, KeyAction 에 이벤트가 구독되었다면 Invoke 로 이벤트를 발생시킨다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Managers : MonoBehaviour
{
static Managers s_instance;
public static Managers Instance { get { Init(); return s_instance;} }
InputManager _input = new InputManager();
public static InputManager Input { get { return Instance._input; } }
// Start is called before the first frame update
void Start()
{
Init();
}
// Update is called once per frame
void Update()
{
_input.OnUpdate();
}
static void Init()
{
if (s_instance == null)
{
GameObject go = GameObject.Find("@Managers");
if (go == null)
{
go = new GameObject { name = "@Managers"};
go.AddComponent<Managers>();
}
DontDestroyOnLoad(go);
s_instance = go.GetComponent<Managers>();
}
}
}
InputManager 는 기존 Managers 에서 인스턴스가 관리될 수 있도록 _input 으로 생성하고 Update 에서 _input의 OnUpdate 를 실행해준다. 즉 기존 매니저에서 InputManager 도 관리될 수 있도록 함.
void Start()
{
Managers.Input.KeyAction -= OnKeyboard;
Managers.Input.KeyAction += OnKeyboard;
}
void OnKeyboard()
{
if (Input.GetKey(KeyCode.W))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
transform.position += Vector3.forward * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
transform.position += Vector3.back * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
transform.position += Vector3.left * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
transform.position += Vector3.right * Time.deltaTime * _speed;
}
//transform
}
PlayerController 에서는 InputManager 의 KeyAction 에 OnKeyboard 함수를 이벤트로 등록한다.
그리고 OnKeyboard 함수에 기존의 이동을 구현했던 코드를 작성해주면 InputManager 를 통해 키보드 입력을 관리할 수 있다.
반응형
'⇥ 2D Game > Unity' 카테고리의 다른 글
Unity 게임 만들기 프로젝트 - 충돌 (Collision) (1) | 2024.06.06 |
---|---|
Unity 게임 만들기 프로젝트 - Resource Manager (2) | 2024.06.05 |
Unity 게임 만들기 프로젝트 - 이동 구현 (Rotate) (0) | 2024.06.05 |
Unity 게임 만들기 프로젝트 - 이동 구현 (transform) (0) | 2024.06.05 |
Unity 게임 만들기 프로젝트 - Manager 만들기 (singleton 패턴) (0) | 2024.06.05 |
댓글
이 글 공유하기
다른 글
-
Unity 게임 만들기 프로젝트 - 충돌 (Collision)
Unity 게임 만들기 프로젝트 - 충돌 (Collision)
2024.06.06 -
Unity 게임 만들기 프로젝트 - Resource Manager
Unity 게임 만들기 프로젝트 - Resource Manager
2024.06.05 -
Unity 게임 만들기 프로젝트 - 이동 구현 (Rotate)
Unity 게임 만들기 프로젝트 - 이동 구현 (Rotate)
2024.06.05 -
Unity 게임 만들기 프로젝트 - 이동 구현 (transform)
Unity 게임 만들기 프로젝트 - 이동 구현 (transform)
2024.06.05