跳到主要内容

Inputs

重要 注意 提醒

Coding Style wiki


InputsInputSystem静态输入调度门面 (Facade),内部由 InputCenter 单例 (internal) 集中管理注册缓存,并区分为两组 API:Inputs.CM (Control Map,管理 Unity New InputSystem 的 IInputActionCollection 控制表) 与 Inputs.IA (Input Action,注册与驱动 IInputAction 通用信号派送者)。

命名空间OxGKit.InputSystem
类型public static class Inputs (内含嵌套 public static class CMpublic static class IA)
源码InputCenter.cs
using OxGKit.InputSystem;

快速上手

using OxGKit.InputSystem;

// 1. 注册 Control Map (Unity New InputSystem 生成的类,注册时默认自动 Enable)
Inputs.CM.RegisterControlMap<PlayerControls>();

// 2. 注册 Input Action (注册时会自动调用 OnCreate 进行事件绑定)
Inputs.IA.RegisterInputAction<PlayerAction>();

// 3. 订阅 Input Action 派送的输入信号
Inputs.IA.GetInputAction<PlayerAction>().onMoveAction += OnMoveActionHandler;

// 4. 由主程序的 MonoBehaviour (Update) 每帧驱动更新
Inputs.IA.DriveUpdate(Time.unscaledDeltaTime);

通用规则

注册顺序

重要 先注册 Control Maps,再注册 Input Actions:Input Action 的 OnCreate 通常会通过 GetControlMap 获取控制表进行绑定,若 Control Map 尚未注册会获取到 null,导致绑定被静默跳过。

驱动更新

重要 应由单一主程序 MonoBehaviour 调用 DriveUpdate:忘记调用会使所有依赖 OnUpdate 的轮询逻辑失效;多处调用则会重复驱动 (double-tick)。

重复注册

注意 注册采用静态缓存:同一类型重复注册会输出警告并以首次注册的实例为准,重启流程 (场景重载) 需自行考量此行为。


Inputs.CM (Control Map)

管理 Unity New InputSystem 的控制表 (Input Action Asset 生成类,泛型约束为 IInputActionCollection)。

方法总览

方法说明
RegisterControlMap注册控制表 (注册时默认自动 Enable)。
GetControlMap获取已注册的控制表。
SetActive开关控制表 (Enable / Disable)。
IsActive检查控制表启用状态。

RegisterControlMap

public static void RegisterControlMap<TIInputActionCollection>() where TIInputActionCollection : IInputActionCollection, new()

注册控制表 (Input Action Asset),内部会以 new() 创建实例并自动 Enable (注册后即可接收输入)。

Inputs.CM.RegisterControlMap<PlayerControls>();

注意 同一类型重复注册会输出警告 [ControlMap] <TypeName> already exist.,并保留首次注册的实例。

GetControlMap

public static TIInputActionCollection GetControlMap<TIInputActionCollection>() where TIInputActionCollection : IInputActionCollection

获取已注册的控制表实例;未注册时返回 default (null),建议搭配 null 检查使用。

var ctrls = Inputs.CM.GetControlMap<PlayerControls>();
if (ctrls != null)
{
ctrls.Player.Move.performed += this.OnMoveAction;
}

SetActive

public static void SetActive<TIInputActionCollection>(bool active) where TIInputActionCollection : IInputActionCollection

开关控制表:true 调用 Enable()false 调用 Disable() (例如 UI 模式或过场动画时禁用玩家输入);未注册时输出错误 [ControlMap] <TypeName> cannot found.

// 禁用整组控制表
Inputs.CM.SetActive<PlayerControls>(false);

// 仅开关单一 Action Map (使用 Unity New InputSystem 原生 API)
Inputs.CM.GetControlMap<PlayerControls>()?.Player.Disable();
Inputs.CM.GetControlMap<PlayerControls>()?.Player.Enable();

IsActive

public static bool IsActive<TIInputActionCollection>() where TIInputActionCollection : IInputActionCollection

检查控制表启用状态;未注册时输出错误并返回 false

bool isActive = Inputs.CM.IsActive<PlayerControls>();

Inputs.IA (Input Action)

注册与驱动 IInputAction (通用信号派送者,不依赖任何输入控制插件)。

方法总览

方法说明
RegisterInputAction注册 Input Action (注册时自动调用 OnCreate)。
GetInputAction获取已注册的 Input Action。
DriveUpdate驱动所有 Input Action 的 OnUpdate (由主程序 Update 调用)。
UpdateInputActions(Obsolete) 已弃用,请改用 DriveUpdate。

RegisterInputAction

public static void RegisterInputAction<TInputAction>() where TInputAction : IInputAction, new()

注册 Input Action,内部会以 new() 创建实例并自动调用 OnCreate (通常在此获取 Control Map 进行事件绑定)。

// 先注册 Control Maps,再注册 Input Actions
Inputs.CM.RegisterControlMap<PlayerControls>();
Inputs.IA.RegisterInputAction<PlayerAction>();

注意 同一类型重复注册会输出警告 [InputAction] <TypeName> already exist.,并保留首次注册的实例。

GetInputAction

public static TInputAction GetInputAction<TInputAction>() where TInputAction : IInputAction

获取已注册的 Input Action 实例 (供订阅与退订事件);未注册时返回 default (null)。

// 订阅输入信号
Inputs.IA.GetInputAction<PlayerAction>().onMoveAction += this._OnMoveAction;

// 退订输入信号
Inputs.IA.GetInputAction<PlayerAction>().onMoveAction -= this._OnMoveAction;

DriveUpdate

public static void DriveUpdate(float dt)

驱动所有已注册 Input Action 的 OnUpdate (传入 dt),由主程序的 MonoBehaviour (Update) 调用。

private void Update()
{
// Call by Main MonoBehaviour (Main Program)
Inputs.IA.DriveUpdate(Time.unscaledDeltaTime);
}

重要 应由单一主程序调用:若依赖 OnUpdate 的轮询逻辑无反应,请先检查是否有每帧调用 DriveUpdate

UpdateInputActions

[Obsolete("UpdateInputActions is deprecated. Use DriveUpdate instead.")]
public static void UpdateInputActions(float dt)

(已弃用) 行为等同 DriveUpdate,请改用 DriveUpdate (v1.0.3 起弃用)。


Logger

本模块的日志器 (OxGKit.LoggingSystemLogging 实现),日志器名称为 OxGKit.InputSystem.Logger,可通过 LoggingLauncher 进行开关与级别配置。

类型[LoggerName("OxGKit.InputSystem.Logger")] public class Logger : Logging
源码Logger.cs

提醒 发布 (Build) 时,需按 LoggingSystem 的规则加入 OXGKIT_LOGGER_ON 宏,本模块的日志才会输出。