[Unity] Input Manager

Yerin·2023년 7월 10일
0

❗ Update 함수에서 입력을 모두 처리할 경우 시스템에 과부하가 생길 수 있다.

-> 키를 체크하는 부분을 따로 관리하여 이벤트를 건네받는 식으로 하는 것이 더욱 효율적이다.

💡 Input 입력을 관리하는 Manager를 만들자!

-> 더욱 더 관리하기 편해짐

PlayerController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//1. 위치 벡터
//2. 방향 벡터

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    float _speed = 10.0f;

    void Start()
    {
        // Input 매니저를 통해 어떠한 키가 눌리면 OnKeyboard 함수가 실행하도록 함
        // 혹시라도 두번 눌릴경우 취소하고 다시 하도록 함
        Managers.Input.KeyAction -= OnKeyboard;
        Managers.Input.KeyAction += OnKeyboard;
    }


    float _yAngle = 0.0f;
    void Update()
    {  

    }

    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;
        }

    }
}

Managers.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Managers : MonoBehaviour
{
    static Managers s_instance; // 유일성이 보장된다.
    static Managers Instance { get { Init(); return s_instance; } } // 유일한 매니저를 갖고온다.

    InputManager _input = new InputManager();
    public static InputManager Input { get { return Instance._input; } }

    void Start()
    {
        Init();
    }

    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.cs

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();
    }
}
profile
재밌는 코딩 공부

0개의 댓글