输入组件

This commit is contained in:
2025-09-05 00:12:46 +08:00
parent f9a9f26009
commit deaca48bf3
6 changed files with 160 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
{
"version": 1,
"name": "PlayerInputControl",
"maps": [
{

View File

@@ -1,9 +1,156 @@
using NBC.Entitas;
using System;
using NBC.Entitas;
using NBC.Entitas.Interface;
using UnityEngine.InputSystem;
namespace NBF.Fishing2
{
public class InputComponent : Entity
{
public PlayerInputControl PlayerInputControl { get; set; }
/// <summary>
/// 执行输入事件
/// </summary>
public event Action<string> OnUIPerformed;
/// <summary>
/// 执行输入事件完毕
/// </summary>
public event Action<string> OnUICanceled;
/// <summary>
/// 执行输入事件
/// </summary>
public event Action<string> OnPlayerPerformed;
/// <summary>
/// 执行输入事件完毕
/// </summary>
public event Action<string> OnPlayerCanceled;
/// <summary>
/// 手柄输入
/// </summary>
public static bool IsControllerInput;
/// <summary>
/// ui阻止游戏输入
/// </summary>
public static bool IsUIStopInput;
public void OnUIButtonPerformed(InputAction.CallbackContext context)
{
OnUIPerformed?.Invoke(context.action.name);
}
public void OnUIButtonCanceled(InputAction.CallbackContext context)
{
OnUICanceled?.Invoke(context.action.name);
}
public 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);
}
public 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 static class InputComponentSystem
{
public class InputComponentAwakeSystem : AwakeSystem<InputComponent>
{
protected override void Awake(InputComponent self)
{
self.PlayerInputControl = new PlayerInputControl();
self.PlayerInputControl.Enable();
foreach (var actionMap in self.PlayerInputControl.asset.actionMaps)
{
actionMap.Enable();
if (actionMap.name == "UI")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed += self.OnUIButtonPerformed;
action.canceled += self.OnUIButtonCanceled;
}
}
}
else if (actionMap.name == "Player")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed += self.OnPlayerButtonPerformed;
action.canceled += self.OnPlayerButtonCanceled;
}
}
}
}
}
}
public class InputComponentDestroySystem : DestroySystem<InputComponent>
{
protected override void Destroy(InputComponent self)
{
foreach (var actionMap in self.PlayerInputControl.asset.actionMaps)
{
actionMap.Enable();
if (actionMap.name == "UI")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed -= self.OnUIButtonPerformed;
action.canceled -= self.OnUIButtonCanceled;
}
}
}
else if (actionMap.name == "Player")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed -= self.OnPlayerButtonPerformed;
action.canceled -= self.OnPlayerButtonCanceled;
}
}
}
}
}
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using NBF.Fishing2;
using UnityEngine;
namespace NBF
@@ -7,15 +8,18 @@ namespace NBF
{
public static Global Instance { get; private set; }
public static InputComponent Input;
/// <summary>
/// 主摄像机
/// </summary>
public Camera MainCamera { get; private set; }
public static Camera MainCamera { get; private set; }
/// <summary>
/// 摄像机配置
/// </summary>
public CameraScriptObject CameraConfig { get; private set; }
public static CameraScriptObject CameraConfig { get; private set; }
private void Awake()
{

View File

@@ -70,7 +70,6 @@ namespace NBF
private void Start()
{
AddEvent();
}

View File

@@ -14,8 +14,10 @@ namespace NBF.Fishing2
scene.AddComponent<ObjectWait>();
scene.AddComponent<MapManageComponent>();
scene.AddComponent<CameraComponent>();
scene.AddComponent<InputComponent>();
var input = scene.AddComponent<InputComponent>();
scene.AddComponent<SettingComponent>();
Global.Input = input;
}
await FTask.CompletedTask;

View File

@@ -24,5 +24,5 @@ EditorBuildSettings:
path: Assets/ResRaw/Maps/Map99/Map99.unity
guid: 956b354d2875715418f574fcf501363e
m_configObjects:
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: d0e3cb5a99f3d324ea14bfe64485b2e4, type: 3}
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 8a314d59348c97b4e96e6eb9b09285a6, type: 3}
m_UseUCBPForAssetBundles: 0