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

@@ -7,6 +7,12 @@ using UnityEngine.InputSystem;
namespace NBF
{
public enum ControllerType
{
KeyboardMouse = 0,
GamePad = 1
}
public class InputManager : MonoBehaviour
{
public static InputManager Instance { get; private set; }
@@ -44,17 +50,62 @@ namespace NBF
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;
@@ -67,17 +118,6 @@ namespace NBF
return PlayerInputControl.Player.Look?.ReadValue<Vector2>() ?? Vector2.zero;
}
private void Start()
{
PlayerInputControl = new PlayerInputControl();
PlayerInputControl.Enable();
AddEvent();
}
private void OnDestroy()
{
RemoveEvent();
}
private void AddEvent()
{
@@ -95,7 +135,7 @@ namespace NBF
}
}
}
else if (actionMap.name == "Normal")
else if (actionMap.name == "Player")
{
foreach (var action in actionMap.actions)
{
@@ -139,6 +179,8 @@ namespace NBF
{
OnOp2Action?.Invoke(true);
}
OnPlayerPerformed?.Invoke(actionName);
}
private void OnPlayerButtonCanceled(InputAction.CallbackContext context)
@@ -153,6 +195,8 @@ namespace NBF
{
OnOp2Action?.Invoke(false);
}
OnPlayerCanceled?.Invoke(actionName);
}

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

View File

@@ -1,27 +1,143 @@
namespace NBF
using System;
using System.Collections.Generic;
using System.Reflection;
using NBC;
using UnityEngine;
namespace NBF
{
public class InputDef
{
public class UI
{
public static Dictionary<string, List<string>> Icons = new Dictionary<string, List<string>>();
[InputIcon("icon_controller_123", "icon_controller_2")]
public const string Back = "Back";
[InputIcon("icon_controller_129", "icon_controller_1")]
public const string Enter = "Enter";
[InputIcon("icon_controller_127", "icon_controller_19")]
public const string Tab = "Tab";
[InputIcon("icon_controller_29", "icon_controller_89")]
public const string Prev = "Prev";
[InputIcon("icon_controller_30", "icon_controller_77")]
public const string Next = "Next";
[InputIcon("icon_controller_98", "icon_controller_27")]
public const string SubPrev = "SubPrev";
[InputIcon("icon_controller_75", " icon_controller_28")]
public const string SubNext = "SubNext";
public const string Left = "Left";
public const string Right = "Right";
public const string Up = "Up";
public const string Down = "Down";
[InputIcon("", "")] public const string Left = "Left";
[InputIcon("", "")] public const string Right = "Right";
[InputIcon("", "")] public const string Up = "Up";
[InputIcon("", "")] public const string Down = "Down";
}
public class Player
public static class Player
{
public static Dictionary<string, List<string>> Icons = new Dictionary<string, List<string>>();
public const string Run = "Run";
public const string Use1 = "Use1";
public const string Use2 = "Use2";
}
#region Load Icon
public static void LoadIcon()
{
// 获取InputDef类型
Type inputDefType = typeof(NBF.InputDef);
// 检查UI类
Type uiType = inputDefType.GetNestedType("UI");
if (uiType != null)
{
var uiFields = GetInputIconFieldsFromType(uiType);
if (uiFields.Count > 0)
{
foreach (var field in uiFields)
{
InputIconAttribute attribute =
(InputIconAttribute)Attribute.GetCustomAttribute(field, typeof(InputIconAttribute));
string value = (string)field.GetValue(null); // 获取常量值
if (attribute != null)
{
List<string> icons = new List<string>
{
attribute.KeyBoardIcon,
attribute.ControllerIcon
};
UI.Icons[value] = icons;
}
}
}
}
// 检查Player类
Type playerType = inputDefType.GetNestedType("Player");
if (playerType != null)
{
var playerFields = GetInputIconFieldsFromType(playerType);
if (playerFields.Count > 0)
{
foreach (var field in playerFields)
{
InputIconAttribute attribute =
(InputIconAttribute)Attribute.GetCustomAttribute(field, typeof(InputIconAttribute));
string value = (string)field.GetValue(null); // 获取常量值
if (attribute != null)
{
List<string> icons = new List<string>
{
attribute.KeyBoardIcon,
attribute.ControllerIcon
};
Player.Icons[value] = icons;
}
}
}
}
// foreach (var key in InputDef.UI.Icons.Keys)
// {
// var icons = InputDef.UI.Icons[key];
// Log.Error($"KEY={key} icon={string.Join(",", icons)}");
// }
}
// 从特定类型获取带有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;
}
#endregion
}
}

View File

@@ -103,12 +103,12 @@ public class FHook : FPlayerGear
private void OnCollisionEnter(Collision collision)
{
if (!waterDisplacement.isInWater && (bool)Owner.Gears.Rod &&
(bool)Owner.Gears.Rod.lineHandler && !Owner.Gears.Rod.currentFish &&
Owner.Data.lineLength > 5f &&
Vector3.Distance(transform.position, Owner.Gears.Rod.transform.position) > 5f)
{
// GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("HOOK_ON_THE_GROUND"), FScriptsHandler.Instance.m_Canvas.transform, deleteLast: true);
}
// if (!waterDisplacement.isInWater && (bool)Owner.Gears.Rod &&
// (bool)Owner.Gears.Rod.lineHandler && !Owner.Gears.Rod.currentFish &&
// Owner.Data.lineLength > 5f &&
// Vector3.Distance(transform.position, Owner.Gears.Rod.transform.position) > 5f)
// {
// GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("HOOK_ON_THE_GROUND"), FScriptsHandler.Instance.m_Canvas.transform, deleteLast: true);
// }
}
}

View File

@@ -55,7 +55,7 @@ namespace NBF
transform.position = _player.Data.position;
transform.rotation = _player.Data.rotation;
App.Inst.SetMouseCurrsor(false);
InputManager.OnPlayerCanceled += OnPlayerCanceled;
InputManager.OnPlayerPerformed += OnPlayerPerformed;
// InputManager.OnRunAction += OnRunAction;
@@ -192,7 +192,7 @@ namespace NBF
{
// Move
Vector2 movementInput = InputManager.GetMovementInput();
Vector3 movementDirection = Vector3.zero;
movementDirection += Vector3.forward * movementInput.y;

View File

@@ -547,7 +547,7 @@ namespace NBF
""id"": ""c1f7a91b-d0fd-4a62-997e-7fb9b69bf235"",
""path"": ""<Gamepad>/rightStick"",
""interactions"": """",
""processors"": """",
""processors"": ""ScaleVector2(x=40,y=40)"",
""groups"": "";Gamepad"",
""action"": ""Look"",
""isComposite"": false,
@@ -1066,7 +1066,7 @@ namespace NBF
""id"": ""348bd961-a6a0-4d13-b414-83b43201bc10"",
""expectedControlType"": """",
""processors"": """",
""interactions"": """",
""interactions"": ""Hold"",
""initialStateCheck"": false
},
{
@@ -1075,7 +1075,7 @@ namespace NBF
""id"": ""da1344ed-a4b5-43ef-bad6-a3f6bb06ff23"",
""expectedControlType"": """",
""processors"": """",
""interactions"": """",
""interactions"": ""Hold"",
""initialStateCheck"": false
},
{
@@ -1084,7 +1084,7 @@ namespace NBF
""id"": ""dafd152b-aa0a-48bb-890d-289ab3a3e713"",
""expectedControlType"": """",
""processors"": """",
""interactions"": """",
""interactions"": ""Hold"",
""initialStateCheck"": false
},
{
@@ -1093,7 +1093,7 @@ namespace NBF
""id"": ""e0dbeebd-c69d-44bd-83de-94fcce8c6727"",
""expectedControlType"": """",
""processors"": """",
""interactions"": """",
""interactions"": ""Hold"",
""initialStateCheck"": false
},
{
@@ -1129,6 +1129,28 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""e834d5a6-95f1-4142-9da6-4cbcc8618342"",
""path"": ""<XInputController>/buttonEast"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Back"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""48a10b34-e3b4-4a90-98a2-95e128a3417a"",
""path"": ""<Gamepad>/buttonEast"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Back"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""a6e94404-e896-4fec-b3bf-affd31b12571"",
@@ -1140,6 +1162,17 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""7028032c-6c7f-4846-8d2c-a41c3c2822fd"",
""path"": ""<XInputController>/buttonEast"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Enter"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""23dac47b-935f-4261-8044-a8612edf3fda"",
@@ -1162,6 +1195,28 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""5e809757-a6a4-4f89-9256-619af1216748"",
""path"": ""<XInputController>/leftShoulder"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Prev"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""5a43b881-410f-4e67-913f-cbc379ae6953"",
""path"": ""<Gamepad>/leftShoulder"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Prev"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""2661bf5d-0afe-4f8c-a4e8-2aa6150c724d"",
@@ -1173,6 +1228,28 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""7250c0b2-9b1c-445e-8c8e-125dcf79e29f"",
""path"": ""<XInputController>/rightShoulder"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Next"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""9fa9e49d-5262-4890-8ba2-6c84695f88aa"",
""path"": ""<Gamepad>/rightShoulder"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Next"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""487ceb23-336d-45f6-a782-3b4e9d9f0cd1"",
@@ -1184,6 +1261,28 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""589ddbb5-aedc-483d-957c-1fd7c1a12002"",
""path"": ""<XInputController>/leftTrigger"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""SubPrev"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""0fe51227-7d8b-4baa-9d25-8396deefc34d"",
""path"": ""<Gamepad>/leftTrigger"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""SubPrev"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""463ec073-6e9d-4b1d-9956-8ec19838aa3e"",
@@ -1195,6 +1294,28 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""2c584e41-80cd-463e-8423-ac74c932c90b"",
""path"": ""<XInputController>/rightTrigger"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""SubNext"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""c8394c43-028f-4056-b5bd-f81395997f15"",
""path"": ""<Gamepad>/rightTrigger"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""SubNext"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""053ddbac-9f73-4672-be59-abfdb6037aea"",
@@ -1206,6 +1327,17 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""2e150513-8adf-4e87-b5c9-cab09870835e"",
""path"": ""<Gamepad>/leftStick/right"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Right"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""031b930d-59b6-4a26-9dfe-97764c3e8b37"",
@@ -1217,6 +1349,17 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""dced9424-e4e1-40d9-8aea-b69491365e74"",
""path"": ""<Gamepad>/leftStick/left"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Left"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""45f78626-cbf2-4e1f-904d-d82e4c2a2f41"",
@@ -1228,6 +1371,17 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""2e119597-8f21-4c82-916c-57ffcd0f34c5"",
""path"": ""<Gamepad>/leftStick/down"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Down"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""f1a47b3c-e79f-4ab3-86b6-fcc0a6470114"",
@@ -1239,6 +1393,17 @@ namespace NBF
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""9a43ed61-260a-4f92-9e49-3df421db4051"",
""path"": ""<Gamepad>/leftStick/up"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Up"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""393f1eb9-d10d-4cd1-87e7-ea299f92b2da"",

View File

@@ -18,7 +18,9 @@ namespace NBC
bool IsDotDel { get; }
bool IsModal { get; }
bool IsShowCursor { get; }
void SetUIManager(UIManager kit);
void SetData(object args);
object GetData();

View File

@@ -35,6 +35,11 @@ namespace NBC
/// </summary>
public virtual int Id { get; protected set; }
/// <summary>
/// 是否显示光标,屏蔽游戏内输入
/// </summary>
public virtual bool IsShowCursor { get; protected set; }
/// <summary>
/// 面板打开动画
/// </summary>

View File

@@ -1,4 +1,6 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using FairyGUI;
using NBC;
@@ -9,12 +11,6 @@ using UnityEngine.Video;
namespace NBF
{
public enum ControllerType
{
KeyboardMouse = 0,
GamePad = 1
}
public class App : MonoBehaviour
{
public static App Inst { get; private set; }
@@ -35,6 +31,11 @@ namespace NBF
// new GameObject("GameManager").AddComponent<GameManager>();
}
private void OnDestroy()
{
PermanentCommon.Dispose();
}
void Start()
{
ES3.Init();
@@ -85,9 +86,11 @@ namespace NBF
public void StartGame()
{
PermanentCommon.Init();
InputDef.LoadIcon();
UI.Inst.OpenUI<FishingShopPanel>();
LoadData();
//Fishing.Inst.Go(1);
// Fishing.Inst.Go(1);
}
private void LoadData()
@@ -118,17 +121,15 @@ namespace NBF
public ControllerType controllerType = ControllerType.GamePad;
public void SetMouseCurrsor(bool val)
public void SetMouseCursor(bool val)
{
if (val)
{
// InputManager inputManager = FindObjectOfType<InputManager>();
if (controllerType == ControllerType.KeyboardMouse)
{
Cursor.visible = true;
}
// inputManager.SetCursorPositionToScreenCenter();
Cursor.lockState = CursorLockMode.None;
}
else if (controllerType == ControllerType.KeyboardMouse)

View File

@@ -24,6 +24,7 @@ namespace NBF
{
base.OnInit();
this.AutoAddClick(OnClick);
IsShowCursor = true;
for (int i = 0; i < 10; i++)
{