213 lines
5.9 KiB
C#
213 lines
5.9 KiB
C#
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<InputManager>
|
|
{
|
|
public static bool IsOp1;
|
|
public static bool IsOp2;
|
|
|
|
public static event Action<bool> OnOp1Action;
|
|
public static event Action<bool> OnOp2Action;
|
|
|
|
/// <summary>
|
|
/// 执行输入事件
|
|
/// </summary>
|
|
public static event Action<string> OnUIPerformed;
|
|
|
|
/// <summary>
|
|
/// 执行输入事件完毕
|
|
/// </summary>
|
|
public static event Action<string> OnUICanceled;
|
|
|
|
/// <summary>
|
|
/// 执行输入事件
|
|
/// </summary>
|
|
public static event Action<string> OnPlayerPerformed;
|
|
|
|
/// <summary>
|
|
/// 执行输入事件完毕
|
|
/// </summary>
|
|
public static event Action<string> OnPlayerCanceled;
|
|
|
|
/// <summary>
|
|
/// 触发交互游戏对象
|
|
/// </summary>
|
|
public static event Action<InteractiveObject> OnInteractiveObjectAction;
|
|
|
|
public static PlayerInputControl PlayerInputControl { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 手柄输入
|
|
/// </summary>
|
|
public static bool IsControllerInput;
|
|
|
|
/// <summary>
|
|
/// ui阻止游戏输入
|
|
/// </summary>
|
|
public static bool IsUIStopInput;
|
|
|
|
public static ControllerType ControllerType = ControllerType.KeyboardMouse;
|
|
|
|
protected override void OnAwake()
|
|
{
|
|
InputCursorExtension.InputInit();
|
|
DontDestroyOnLoad(gameObject);
|
|
PlayerInputControl = new PlayerInputControl();
|
|
PlayerInputControl.Enable();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
|
|
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>() ?? Vector2.zero;
|
|
}
|
|
|
|
public static Vector2 GetLookInput()
|
|
{
|
|
if (IsUIStopInput) return Vector2.zero;
|
|
return PlayerInputControl.Player.Look?.ReadValue<Vector2>() ?? 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);
|
|
}
|
|
}
|
|
} |