using System; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.Windows; public enum InputState { Started, Performing, Canceled } public class InputManager : MonoBehaviour { public static InputManager Instance; private PlayerInput _playerInput; private InputActionMap _globalInputActionMap; private InputActionMap _uiInputActionMap; private InputActionMap _characterInputActionMap; //콜백 이벤트들 //캐릭터 조작관련은 따로 분류 public event Action OnMouseScrollEvent; public event Action OnMoveEvent; public event Action OnSprintEvent; public event Action OnJumpEvent; public event Action OnNormalAttackEvent; public event Action OnHeavyAttackEvent; //키조작 public event Action OnKeyDown_UpArrowEvent; public event Action OnKeyDown_DownArrowEvent; public event Action OnKeyDown_EnterEvent; private void Awake() { if (Instance == null) { Instance = this; //만들어진 자신을 인스턴스로 설정 } else { Destroy(gameObject); //이미 인스턴스가 있으면 자신을 파괴 } _playerInput = GetComponent(); } private void Start() { SetGlobalInputMap("Global"); } public void UnPairDevices() { if(_playerInput != null) { _playerInput.user.UnpairDevices(); PlayerInputEnable(false); } } public void PlayerInputEnable(bool flag) { _playerInput.enabled = flag; } private void SetGlobalInputMap(string mapName) { _globalInputActionMap = _playerInput?.actions?.FindActionMap(mapName); } public void SetUIInputMap(string mapName) { _uiInputActionMap = _playerInput?.actions?.FindActionMap(mapName); if (_uiInputActionMap == null) return; // 맵 활성화 _uiInputActionMap.Enable(); //바인딩 BindActionUI("OnKeyDown_UpArrow", OnKeyDown_UpArrow); BindActionUI("OnKeyDown_DownArrow", OnKeyDown_DownArrow); BindActionUI("OnKeyDown_Enter", OnKeyDown_Enter); } public void SetCharacterInputMap(string mapName) { _characterInputActionMap = _playerInput?.actions?.FindActionMap(mapName); if (_characterInputActionMap == null) return; // 맵 활성화 _characterInputActionMap.Enable(); //바인딩 BindActionCharacter("Scroll", OnMouseScroll); BindActionCharacter("Move", OnMove); BindActionCharacter("Sprint", OnSprint); BindActionCharacter("Jump", OnJump); BindActionCharacter("NormalAttack", OnNormalAttack); BindActionCharacter("HeavyAttack", OnHeavyAttack); } //매핑용 함수 private void BindActionUI(string actionName, Action callback) { InputAction action = _uiInputActionMap.FindAction(actionName); if (action != null) { action.performed -= callback; action.canceled -= callback; action.started -= callback; action.performed += callback; action.canceled += callback; action.started += callback; action.Enable(); } } private void BindActionCharacter(string actionName, Action callback) { InputAction action = _characterInputActionMap.FindAction(actionName); if (action != null) { action.performed -= callback; action.canceled -= callback; action.started -= callback; action.performed += callback; action.canceled += callback; action.started += callback; action.Enable(); } } #region 캐릭터 조작 private void OnMouseScroll(InputAction.CallbackContext ctx) { Vector2 scroll = ctx.ReadValue(); OnMouseScrollEvent?.Invoke(scroll.y); } private void OnMove(InputAction.CallbackContext ctx) { Vector2 move = ctx.ReadValue(); OnMoveEvent?.Invoke(move); } private void OnSprint(InputAction.CallbackContext ctx) { if(ctx.performed) { OnSprintEvent?.Invoke(InputState.Performing); } if(ctx.canceled) { OnSprintEvent?.Invoke(InputState.Canceled); } } private void OnJump(InputAction.CallbackContext ctx) { if(ctx.started) { OnJumpEvent?.Invoke(InputState.Started); } if (ctx.canceled) { OnJumpEvent?.Invoke(InputState.Canceled); } } private void OnNormalAttack(InputAction.CallbackContext ctx) { OnNormalAttackEvent?.Invoke(); } private void OnHeavyAttack(InputAction.CallbackContext ctx) { OnHeavyAttackEvent?.Invoke(); } #endregion #region 키별 조작 private void OnKeyDown_UpArrow(InputAction.CallbackContext ctx) { if(ctx.started) OnKeyDown_UpArrowEvent?.Invoke(); } private void OnKeyDown_DownArrow(InputAction.CallbackContext ctx) { if (ctx.started) OnKeyDown_UpArrowEvent?.Invoke(); } private void OnKeyDown_Enter(InputAction.CallbackContext ctx) { if (ctx.started) OnKeyDown_EnterEvent?.Invoke(); } #endregion }