input相关

This commit is contained in:
2025-05-26 00:06:37 +08:00
parent 68b88d57db
commit 763fa5103f
23 changed files with 745 additions and 68 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7cb50811095d496fad57afd7535d0f73
timeCreated: 1748180763

View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace NBF
{
public static class AttributeHelper
{
// 获取特定嵌套类中带有InputIconAttribute的常量字段
public static Dictionary<string, List<FieldInfo>> GetNestedClassInputIconFields()
{
var result = new Dictionary<string, List<FieldInfo>>();
// 获取InputDef类型
Type inputDefType = typeof(NBF.InputDef);
// 检查UI类
Type uiType = inputDefType.GetNestedType("UI");
if (uiType != null)
{
var uiFields = GetInputIconFieldsFromType(uiType);
if (uiFields.Count > 0)
{
result.Add("UI", uiFields);
}
}
// 检查Player类
Type playerType = inputDefType.GetNestedType("Player");
if (playerType != null)
{
var playerFields = GetInputIconFieldsFromType(playerType);
if (playerFields.Count > 0)
{
result.Add("Player", playerFields);
}
}
return result;
}
// 从特定类型获取带有InputIconAttribute的常量字段
private static List<FieldInfo> GetInputIconFieldsFromType(Type type)
{
List<FieldInfo> result = new List<FieldInfo>();
// 只获取公共常量字段
FieldInfo[] fields = type.GetFields(
BindingFlags.Public |
BindingFlags.Static |
BindingFlags.FlattenHierarchy);
foreach (FieldInfo field in fields)
{
// 检查是否是常量且带有InputIconAttribute
if (field.IsLiteral && !field.IsInitOnly &&
Attribute.IsDefined(field, typeof(InputIconAttribute)))
{
result.Add(field);
}
}
return result;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fa024ae7ea104f809ace6beff59c12d8
timeCreated: 1748188551

View File

@@ -0,0 +1,22 @@
using System;
namespace NBF
{
[AttributeUsage(AttributeTargets.Field)]
public class InputIconAttribute : Attribute
{
public string KeyBoardIcon;
public string ControllerIcon;
/// <summary>
///
/// </summary>
/// <param name="keyBoardIcon">键盘图标</param>
/// <param name="controllerIcon">控制器图标</param>
public InputIconAttribute(string keyBoardIcon, string controllerIcon)
{
KeyBoardIcon = keyBoardIcon;
ControllerIcon = controllerIcon;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5fb17097160c43ee9840411aded17084
timeCreated: 1748180773

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d846463d130b4191a5a4a9d2e2dfe45b
timeCreated: 1748180579

View File

@@ -0,0 +1,46 @@
using NBC;
namespace NBF
{
public static class InputCursorExtension
{
public static void InputInit()
{
UI.Inst.On(UIEvents.UIShow, UIShow, null, 1);
UI.Inst.On(UIEvents.UIHide, UIHide, null, 1);
}
public static void Dispose()
{
}
private static void UIShow(EventArgs ev)
{
CheckUICursor();
}
private static void UIHide(EventArgs ev)
{
CheckUICursor();
}
private static void CheckUICursor()
{
var uis = UI.Inst.GetAllUI();
bool showCursor = false;
foreach (var ui in uis)
{
if (!ui.IsShowing) continue;
if (ui.IsShowCursor)
{
showCursor = true;
break;
}
}
Log.Error($"showCursor={showCursor}");
InputManager.IsUIStopInput = showCursor;
InputManager.SetMouseCursor(showCursor);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0644b8eb12e44351ab75b467f90d4c32
timeCreated: 1748185434

View File

@@ -0,0 +1,209 @@
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 : 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>
/// 手柄输入
/// </summary>
public static bool IsControllerInput;
/// <summary>
/// ui阻止游戏输入
/// </summary>
public static bool IsUIStopInput;
public static ControllerType ControllerType = ControllerType.KeyboardMouse;
private void Awake()
{
Instance = this;
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>() ?? 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);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fecc50928be248b896f6ee0a17e7b76d
timeCreated: 1746892722

View File

@@ -0,0 +1,30 @@
using NBC;
using UnityEngine;
namespace NBF
{
/// <summary>
/// 常驻公共脚本
/// </summary>
public class PermanentCommon
{
private static bool _init;
private static PermanentCommon _inst;
public static PermanentCommon Inst
{
get { return _inst ??= new PermanentCommon(); }
}
public static void Init()
{
if (_init) return;
_init = true;
}
public static void Dispose()
{
_init = false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3816467cfd1648e685a33ebfd1d9fb7d
timeCreated: 1748184924