ProjectDDD/Assets/_Datas/02.Scripts/Controllers/KeyManager.cs
2025-07-01 19:47:26 +09:00

254 lines
7.6 KiB (Stored with Git LFS)
C#

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
namespace DDD
{
public enum InputActionMaps
{
None = 0,
Ui = 1,
Restaurant = 2,
}
public static class RestaurantActions
{
public const string Move = "Move";
public const string Dash = "Dash";
public const string Interact = "Interact";
}
public class KeyManager : Singleton<KeyManager>
{
private PlayerInput _currentPlayerInput;
protected override void OnAwake()
{
_currentPlayerInput = GetComponent<PlayerInput>();
}
private void Start()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
string currentSceneName = SceneManager.GetActiveScene().name;
switch (currentSceneName)
{
case "00.Title":
SwitchCurrentActionMap(InputActionMaps.Ui);
break;
case "01.Restaurant":
SwitchCurrentActionMap(InputActionMaps.Restaurant);
break;
default:
throw new System.Exception("Invalid scene name");
}
}
private bool IsNullCurrentPlayerInput()
{
if (_currentPlayerInput && _currentPlayerInput.enabled) return false;
Debug.Log("CurrentPlayerInput가 할당되지 않았습니다.");
return true;
}
public InputAction GetAction(InputActionMaps actionMapName, string actionName)
{
if (IsNullCurrentPlayerInput()) return null;
var actionMap = _currentPlayerInput.actions.FindActionMap(actionMapName.ToString(), true);
if (actionMap == null)
{
Debug.LogError($"Action Map '{actionMapName}' not found!");
return null;
}
var action = actionMap.FindAction(actionName, true);
if (action == null)
{
Debug.LogError($"Action '{actionName}' not found in Action Map '{actionMapName}'!");
}
return action;
}
public string GetBoundKey(InputActionMaps actionMapName, string actionName)
{
if (IsNullCurrentPlayerInput()) return null;
var actionMap = _currentPlayerInput.actions.FindActionMap(actionMapName.ToString(), true);
if (actionMap == null)
{
Debug.LogError($"Action Map '{actionMapName}' not found!");
return null;
}
var action = actionMap.FindAction(actionName, true);
if (action == null)
{
Debug.LogError($"Action '{actionName}' not found in Action Map '{actionMapName}'!");
return null;
}
// 첫 번째 바인딩에서 키 이름 가져오기
foreach (var binding in action.bindings)
{
if (!string.IsNullOrEmpty(binding.path))
{
// 키 이름만 추출
var key = InputControlPath.ToHumanReadableString(binding.path,
InputControlPath.HumanReadableStringOptions.OmitDevice);
return key;
}
}
Debug.LogWarning($"No bindings found for action '{actionName}' in Action Map '{actionMapName}'.");
return null;
}
public string GetBoundKey(InputAction inputAction)
{
if (IsNullCurrentPlayerInput()) return null;
if (inputAction == null)
{
Debug.LogError($"Action not found'!");
return null;
}
// 첫 번째 바인딩에서 키 이름 가져오기
foreach (var binding in inputAction.bindings)
{
if (!string.IsNullOrEmpty(binding.path))
{
// 키 이름만 추출
var key = InputControlPath.ToHumanReadableString(binding.path,
InputControlPath.HumanReadableStringOptions.OmitDevice);
return key;
}
}
Debug.LogWarning($"No bindings found for action '{inputAction}'");
return null;
}
public bool IsCurrentActionMap(InputActionMaps inputActionMaps)
{
if (IsNullCurrentPlayerInput()) return false;
return _currentPlayerInput.currentActionMap.ToString() == inputActionMaps.ToString();
}
public void SwitchCurrentActionMap(string inputActionMaps)
{
if (IsNullCurrentPlayerInput()) return;
_currentPlayerInput.SwitchCurrentActionMap(inputActionMaps);
}
public void SwitchCurrentActionMap(InputActionMaps inputActionMaps)
{
if (IsNullCurrentPlayerInput()) return;
_currentPlayerInput.SwitchCurrentActionMap(inputActionMaps.ToString());
}
public InputActionMap GetCurrentInputActionMap()
{
if (IsNullCurrentPlayerInput()) return null;
return _currentPlayerInput.currentActionMap;
}
public void EnableCurrentPlayerInput()
{
if (!_currentPlayerInput) return;
_currentPlayerInput.enabled = true;
}
public void DisableCurrentPlayerInput()
{
if (IsNullCurrentPlayerInput()) return;
_currentPlayerInput.enabled = false;
}
public void DisableAllActionMaps()
{
if (IsNullCurrentPlayerInput()) return;
foreach (var element in _currentPlayerInput.actions.actionMaps)
{
element.Disable();
}
}
public void DisableAllActionsExcept(string exceptActionName)
{
if (IsNullCurrentPlayerInput()) return;
var exceptAction = _currentPlayerInput.currentActionMap.FindAction(exceptActionName);
foreach (var action in _currentPlayerInput.currentActionMap.actions)
{
if (action != exceptAction)
{
action.Disable();
}
else
{
action.Enable();
}
}
}
public void EnableAllActionsMaps()
{
if (IsNullCurrentPlayerInput()) return;
foreach (var action in _currentPlayerInput.actions)
{
action.Enable();
}
}
public void EnableAction(string actionName)
{
if (IsNullCurrentPlayerInput()) return;
var action = _currentPlayerInput.currentActionMap.FindAction(actionName);
if (action == null)
{
Debug.Log($"현재 Action Map인 {_currentPlayerInput.currentActionMap}에는 {actionName} Action이 존재하지 않습니다");
return;
}
action.Enable();
}
public void DisableAction(string actionName)
{
if (IsNullCurrentPlayerInput()) return;
var action = _currentPlayerInput.currentActionMap.FindAction(actionName);
if (action == null)
{
Debug.Log($"현재 Action Map인 {_currentPlayerInput.currentActionMap}에는 {actionName} Action이 존재하지 않습니다");
return;
}
action.Disable();
}
}
}