using System; using System.Runtime.InteropServices; using NBC; // using Rewired; using UnityEngine; using UnityEngine.InputSystem; namespace NBF { public enum ControllerType { KeyboardMouse = 0, GamePad = 1 } public class InputManager : MonoService { public static bool IsOp1; public static bool IsOp2; public static event Action OnOp1Action; public static event Action OnOp2Action; /// /// 执行输入事件 /// public static event Action OnUIPerformed; /// /// 执行输入事件完毕 /// public static event Action OnUICanceled; /// /// 执行输入事件 /// public static event Action OnPlayerPerformed; /// /// 执行输入事件完毕 /// public static event Action OnPlayerCanceled; /// /// 触发交互游戏对象 /// public static event Action OnInteractiveObjectAction; public static PlayerInputControl PlayerInputControl { get; private set; } /// /// 手柄输入 /// public static bool IsControllerInput; /// /// ui阻止游戏输入 /// public static bool IsUIStopInput; public static ControllerType ControllerType = ControllerType.KeyboardMouse; protected override void OnAwake() { InputCursorExtension.InputInit(); DontDestroyOnLoad(gameObject); } private void Start() { PlayerInputControl = new PlayerInputControl(); PlayerInputControl.Enable(); AddEvent(); } private void OnDestroy() { RemoveEvent(); InputCursorExtension.Dispose(); } public static void SetMouseCursor(bool val) { if (val) { if (ControllerType == ControllerType.KeyboardMouse) { Cursor.visible = true; } Cursor.lockState = CursorLockMode.None; } else if (ControllerType == ControllerType.KeyboardMouse) { Cursor.visible = false; } Cursor.visible = val; if (!val) { Cursor.lockState = CursorLockMode.Confined; } } public static Vector2 GetMovementInput() { if (IsUIStopInput) return Vector2.zero; return PlayerInputControl.Player.Move?.ReadValue() ?? Vector2.zero; } public static Vector2 GetLookInput() { if (IsUIStopInput) return Vector2.zero; return PlayerInputControl.Player.Look?.ReadValue() ?? Vector2.zero; } private void AddEvent() { foreach (var actionMap in PlayerInputControl.asset.actionMaps) { actionMap.Enable(); if (actionMap.name == "UI") { foreach (var action in actionMap.actions) { if (action.type == InputActionType.Button) { action.performed += OnUIButtonPerformed; action.canceled += OnUIButtonCanceled; } } } else if (actionMap.name == "Player") { foreach (var action in actionMap.actions) { if (action.type == InputActionType.Button) { action.performed += OnPlayerButtonPerformed; action.canceled += OnPlayerButtonCanceled; } } } } } private void RemoveEvent() { OnUIPerformed = null; OnUICanceled = null; OnPlayerPerformed = null; OnPlayerCanceled = null; } private void OnUIButtonPerformed(InputAction.CallbackContext context) { OnUIPerformed?.Invoke(context.action.name); } private void OnUIButtonCanceled(InputAction.CallbackContext context) { OnUICanceled?.Invoke(context.action.name); } private void OnPlayerButtonPerformed(InputAction.CallbackContext context) { if (IsUIStopInput) return; var actionName = context.action.name; if (actionName == "Op1") { OnOp1Action?.Invoke(true); } else if (actionName == "Op2") { OnOp2Action?.Invoke(true); } OnPlayerPerformed?.Invoke(actionName); } private void OnPlayerButtonCanceled(InputAction.CallbackContext context) { if (IsUIStopInput) return; var actionName = context.action.name; if (actionName == "Op1") { OnOp1Action?.Invoke(false); } else if (actionName == "Op2") { OnOp2Action?.Invoke(false); } OnPlayerCanceled?.Invoke(actionName); } public void OnInteractiveObject(InteractiveObject interactiveObject) { Debug.LogError($"OnInteractiveObject {interactiveObject != null}"); OnInteractiveObjectAction?.Invoke(interactiveObject); } public void SendUIInput(string actionName) { OnUIPerformed?.Invoke(actionName); OnUICanceled?.Invoke(actionName); } } }