165 lines
4.7 KiB
C#
165 lines
4.7 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using NBC;
|
|
// using Rewired;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace NBF
|
|
{
|
|
public class InputManager : MonoBehaviour
|
|
{
|
|
public static InputManager Instance { get; private set; }
|
|
|
|
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>
|
|
/// ui阻止游戏输入
|
|
/// </summary>
|
|
public static bool IsUIStopInput;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
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 Start()
|
|
{
|
|
PlayerInputControl = new PlayerInputControl();
|
|
PlayerInputControl.Enable();
|
|
AddEvent();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
RemoveEvent();
|
|
}
|
|
|
|
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 == "Normal")
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
public void OnInteractiveObject(InteractiveObject interactiveObject)
|
|
{
|
|
Debug.LogError($"OnInteractiveObject {interactiveObject != null}");
|
|
OnInteractiveObjectAction?.Invoke(interactiveObject);
|
|
}
|
|
}
|
|
} |