본문 바로가기
게임 엔진 - 유니티/[루키스] 유니티

[유니티 Note] 섹션 3-4

by 묻공러 2025. 6. 25.

# InputManager

- InputManager의 필요성

Update에 직접 코드가 작성되는 것은 좋지 못함

클래스로 관리해서 유일성 보장 및 유지보수 상승

 

- InputManager 만들기

유니티에서 사용할 클래스가 아니기에

MonoBehaviour 상속도 필요가 없다

 

델리게이트를 사용해 InputManager를 구현하며 된다

참고로 델리게이트 바인드를 사용하는 경우에는

'-='를 통해 모두 끊어주고 '+='를 통해 연결해서 사용하는 것이 안전하다

public class InputManager
{
	public Action KeyAction = null;
    
    public void OnUpdate()
    {
    	if(Input.anykey == false)
       		return;
            
        if(KeyAction != null)
        	KeyAction.Invoke();

    }
}

 

- Managers에 InputManager 포함시키기

기존 Managers 타입의 Instance로의 접근은 막고

InputManager와 같이 Managers에 포함된 클래스들에만 접근이 가능하도록

변경하는 것이 유지보수에 편하다 

그렇기에 Instance는 public을 제거하고

InputManager와 같은 클래스들을 public으로 열어주도록 변경하면 된다

public class Managers : MonoBehaviour
{
	static Managers s_instance;
    static Managers Instance { get { Init(); return s_instance; } } // private 제거
    
    InputManager _input = new InputManager(); // InputManager 생성
    public static InputManager Input { get { return Instance._input; } } // InputManager 프로퍼티

	void Start()
    {
    	Init();
    }
    
    void Update()
    {
    	_input.OnUpdate(); // 이렇게 InputManager의 함수들을 관리
    }
    
    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>();
        }
    }
}

 

- PlayerController 수정

PlayerController 클래스에서

Update에서 하드 코딩 했던 부분들을 함수로 이동시키고

해당 함수를 InputManager의 델리게이트와 바인드를 진행하면 된다

public class PlayerContorller : MonoBehaviour
{
    float _rotationSpeed = 0.2f;
    float _positionSpeed = 10.0f;

    void Start()
    {
        Managers.Input.KeyAction -= OnKeyboard;
        Managers.Input.KeyAction += OnKeyboard;
    }

    void Update()
    {
    
    }

    void OnKeyboard()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.rotation = Quaternion.Slerp(
                transform.rotation,
                Quaternion.LookRotation(Vector3.forward,
                Mathf.Clamp01(_rotationSpeed * Time.deltaTime));

            transform.position += Vector3.forward * Time.deltaTime * _positionSpeed;
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.rotation = Quaternion.Slerp(
                transform.rotation,
                Quaternion.LookRotation(Vector3.back,
                Mathf.Clamp01(_rotationSpeed * Time.deltaTime));

            transform.position += Vector3.back * Time.deltaTime * _positionSpeed;
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.rotation = Quaternion.Slerp(
                transform.rotation,
                Quaternion.LookRotation(Vector3.left,
                Mathf.Clamp01(_rotationSpeed * Time.deltaTime));

            transform.position += Vector3.left * Time.deltaTime * _positionSpeed;
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.rotation = Quaternion.Slerp(
                transform.rotation,
                Quaternion.LookRotation(Vector3.right,
                Mathf.Clamp01(_rotationSpeed * Time.deltaTime));

            transform.position += Vector3.right * Time.deltaTime * _positionSpeed;
        }
    }


}

'게임 엔진 - 유니티 > [루키스] 유니티' 카테고리의 다른 글

[유니티 Note] 섹션 4-2  (0) 2025.06.25
[유니티 Note] 섹션 4-1  (0) 2025.06.25
[유니티 Note] 섹션 3-3  (0) 2025.06.25
[유니티 Note] 섹션 3-2  (0) 2025.06.24
[유니티 Note] 섹션 3-1  (0) 2025.06.24