Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- c++ 문자열 자르기
- LookRotation
- 깊이 우선 탐색
- 유니티
- c++
- 오브젝트 풀링
- 유니티 오브젝트 풀링
- 알고스팟
- 유니티 리소스매니저
- Mathf.Clamp
- InputManager
- 유니티 시야 가림
- 소스코드 줄번호
- 코드블럭 테마
- git-lfs
- Raycast
- LayerMask
- 유니티 Rotate
- Quaternion.Euler
- ML-Agents
- 유니티 InputManager
- 유니티 ResourceManager
- unity
- 유니티 머신러닝
- 너비 우선 탐색
- 유니티 Vector3
- eulerAngles
- 유니티 Collision
- 이진트리
- LFS
Archives
- Today
- Total
무민은귀여워
[유니티] InputManager 본문
반응형
입력처리를 위한 InputManger
- 다른 곳에서 (PlayerController 등) 여러번 입력 체크하지 않고, 매니저에서 한번 체크한 뒤 이벤트로 전파하는 방식을 가능하게 한다
- 입력을 update문에서 직접 처리하면, 코드양이 많아졌을 때 입력 출처를 알기 어려움
1. InputManager 만들기
Managers에서 사용되므로 굳이 MonoBehaviour 로 만들지 않아도 된다
using System; public class InputManager { public Action KeyAction = null; public void OnUpdate() { if (Input.anyKey == false) return; if (KeyAction != null) KeyAction.Invoke(); } }
2. Managers 에 추가
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; } } // Start is called before the first frame update void Start() { Init(); } // Update is called once per frame void Update() { _input.OnUpdate(); } ...
3. PlayerController 에 함수등록
public class PlayerController : MonoBehaviour { [SerializeField] float _speed = 10.0f; 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)) { ...
반응형
'IT > Unity' 카테고리의 다른 글
[유니티] 충돌 Collision / Trigger / Raycast / LayerMask (0) | 2022.05.17 |
---|---|
[유니티] ResourceManager (0) | 2022.05.17 |
[유니티] 회전 rotation (0) | 2022.05.16 |
[유니티] transform, Vector3 (0) | 2022.05.16 |
[유니티] 싱글톤 패턴 (0) | 2022.05.16 |