🎮 Unity 新 Input System 学习笔记
🔧 Input System 环境配置与启用
核心目标:完成新 Input System 的工程级启用与输入模式选择。
- 通过 Package Manager 导入 Input System 包
- 配置路径:
File → Build Settings → Player Settings → Other Settings → Active Input Handling - 可选模式:
- Input Manager(旧)
- Input System(新)
- Both(混合模式,教学与过渡期常用)
- 切换模式后 Unity 会自动重启
工程认知:
Active Input Handling 决定底层输入 API 是否可用,这是所有后续 Input System 功能的前置条件。
⌨️ 键盘设备级输入(Keyboard)
核心目标:理解 Input System 的“设备对象”模型。
- 获取当前键盘设备:
Keyboard keyboard = Keyboard.current; - 单键状态检测:
wasPressedThisFrame:按下wasReleasedThisFrame:抬起isPressed:持续按住
- 示例:A 键、Enter 键、Shift 键
- 任意键监听:
Keyboard.current.anyKey.wasPressedThisFrame - 文本输入事件:
keyboard.onTextInput += (char c) => { };
关键认知:
Input System 不再“轮询字符串键名”,而是通过 强类型设备对象 + 控件状态 进行输入检测。
🖱️ 鼠标设备级输入(Mouse)
核心目标:掌握 Mouse 设备的按键与位移数据读取。
- 获取鼠标设备:
Mouse mouse = Mouse.current; - 鼠标按钮:
leftButtonrightButtonmiddleButtonforwardButton/backButton
- 状态检测:按下 / 抬起 / 长按
- 空间数据读取:
mouse.position.ReadValue():屏幕坐标mouse.delta.ReadValue():位移mouse.scroll.ReadValue():滚轮
⚙️ InputAction 核心机制(代码直连方式)
1. InputAction 的本质
- InputAction 是对“输入意图”的抽象,而不是具体设备
- 支持 Inspector 配置与代码监听
- 解耦“输入来源”与“逻辑响应”
2. Action 类型
- Value:连续值(如移动、摇杆)
- Button:按钮触发
- Pass Through:多设备同时输入
3. Control Type(控制类型)
- Vector2(移动)
- Stick(摇杆)
- Touch(触屏)
- Axis / Digital
4. Interactions(交互器)
- Hold(长按)
- Tap(快速点击)
- SlowTap(慢速点击)
- MultiTap(多击)
- Press(Press / Release / Both)
阶段:started → performed → canceled
5. Processors(值处理器)
- Clamp / Scale / Normalize
- Invert / Invert Vector2 / Vector3
- Axis Deadzone / Stick Deadzone
6. Composite Bindings(复合输入)
- 1D Axis(正负轴)
- 2D Vector(WASD / 方向键)
- 3D Vector(飞行)
- Button + Modifier(Ctrl + C)
7. 生命周期与回调
- 启用:
action.Enable(); - 回调阶段:
started/performed/canceled - CallbackContext 可获取:
- action 名称
- control 名称
- 输入值
- 持续时间与开始时间
📂 InputActions 配置文件体系
- InputActions 是 InputAction 的集合
- 支持多 Action Map(行为分组)
编辑流程
- Project 窗口创建 InputActions 文件
- 双击打开编辑器
- 配置 Action Maps / Actions / Bindings
编辑器核心区域
- Action Maps:行为分组
- Actions:具体输入动作
- Properties:绑定属性
- Toolbar:控制方案(KeyboardMouse / Gamepad / Touch / XR)、Save / Auto-Save
工程认知:
InputActions 文件是 PlayerInput、多人输入、重绑定系统的根基。
📑 Input Actions 配置文件基础
1. Input Actions 是什么
.inputactions文件是 Unity 新 Input System 的核心配置文件- 描述:行为(Action)、行为映射(Action Map)、控制方案(Control Scheme)、设备与按键绑定
- 目标:行为驱动输入,而不是按键驱动输入
2. 基本结构
- Action Maps:一组输入行为(如 Player、UI)
- Actions:具体行为(Move / Look / Fire / Jump 等)
- Bindings:某个行为对应的输入设备与按键
🧾 方式一:生成 C# 类并手动监听
1. 生成 C# 类
- 在
.inputactions文件 Inspector 中勾选 Generate C# Class - 设置类名(如
Lesson9_Input) - Apply 后生成封装类
2. 使用示例
Lesson9_Input input;
void Start()
{
input = new Lesson9_Input();
input.Enable();
input.Action1.Fire.performed += (c) =>
{
Debug.Log("开火");
};
input.Action2.Space.performed += (c) =>
{
Debug.Log("跳跃");
};
}
void Update()
{
Vector2 move = input.Action1.Move.ReadValue<Vector2>();
Debug.Log(move);
}
3. 特点分析
- 优点:代码可控性最高,适合框架封装
- 缺点:需手动 Enable/Disable,事件注册与解绑要自己管理
🎛️ 方式二:PlayerInput 组件概述
1. PlayerInput 是什么
- 高级输入组件
- 自动解析 Input Actions
- 自动管理设备分配
- 自动调用响应函数
2. 使用流程
- 创建 Input Actions 文件
- 在玩家对象上添加 PlayerInput
- 绑定 Input Actions
- 选择输入通知方式(Behavior)
- 编写响应逻辑
3. 核心参数
- Actions:关联的 Input Actions 文件
- Default Action Map:默认启用的行为映射
- Default Control Scheme:默认控制方案
- Camera:多人分屏时指定摄像机
- Behavior:决定输入事件如何通知代码逻辑
🔔 PlayerInput 的四种事件通知模式
方式一:Send Messages
public void OnMove(InputValue value)
{
Vector2 dir = value.Get<Vector2>();
}
- 简单,反射调用,性能一般
- ⚠️ 只有值变化时才回调
方式二:Broadcast Messages
- 与 Send Messages 相同,但可挂在子对象上
方式三:Invoke Unity Events
public void MyFire(InputAction.CallbackContext context)
{
Debug.Log("Fire");
}
- 无反射,可视化配置,适合非程序人员
方式四:Invoke C# Events
PlayerInput input = GetComponent<PlayerInput>();
input.onActionTriggered += OnActionTrigger;
public void OnActionTrigger(InputAction.CallbackContext context)
{
switch (context.action.name)
{
case "Move":
Vector2 dir = context.ReadValue<Vector2>();
break;
case "Fire":
Debug.Log("开火");
break;
}
}
- 性能最好,集中处理所有输入,工程级推荐
📊 InputValue vs CallbackContext
| 对比项 | InputValue | CallbackContext |
|---|---|---|
| 使用场景 | Send / Broadcast | UnityEvent / C# Event |
| 是否持续 | 否 | 是 |
| 可读信息 | 少 | 多 |
| 推荐程度 | 一般 | 强烈推荐 |
👥 PlayerInputManager(本地多人)
1. 作用
- 管理本地多人输入
- 处理玩家加入、离开、设备分配
2. 核心参数
- Join Behavior:加入机制
- Player Prefab:必须挂 PlayerInput
- Split Screen:分屏设置
3. 使用示例
PlayerInputManager.instance.onPlayerJoined +=

浙公网安备 33010602011771号