【MRTK3踩坑记录】Unity 2022 中 MRTK3 Input Simulator 无法使用 WASD 控制相机的完整排查记录


Unity 2022 中 MRTK3 Input Simulator 无法使用 WASD 控制相机的完整排查记录

关键词:Unity 2022 LTS、MRTK3、Input System、Input Simulator、WASD 无法移动、OpenXR

测试环境:Unity 2022.3.x + MRTK3+ OpenXR

目标:恢复 MRTK Input Simulator 的 WASD 相机移动功能

文章目录

  • [Unity 2022 中 MRTK3 Input Simulator 无法使用 WASD 控制相机的完整排查记录](#Unity 2022 中 MRTK3 Input Simulator 无法使用 WASD 控制相机的完整排查记录)

一、问题背景:手势模拟正常,但相机无法移动

在从 Unity 2021 LTS 升级到 Unity 2022 LTS 后,我发现在使用 MRTK Input Simulator 进行编辑器调试时出现了奇怪的现象:

  • 手势模拟(按住 Left Shift + 鼠标)依然正常;

  • 但键盘移动(W、A、S、D、Q、E)以及鼠标右键移动相机这些功能完全无效;

  • 相同的工程在 Unity 2021 中一切正常;

  • InputActions 配置一致 ,没有做额外修改。

这类问题最让人头大:看起来所有配置都对,但就是没反应。


二、初步判断:问题出在输入系统兼容层

Unity 2022 对输入系统的内部实现做了不少改动。

而 MRTK3 的 Input Simulator 是混合使用 旧输入系统(Input.GetKey)新 Input System 的组件。

简而言之:

Unity 2021 中旧 API 还能捕获键盘输入;

Unity 2022 中,若项目使用 "New Input System Only",旧 API 输入被拦截了。

于是我首先怀疑:

  • 是不是 旧输入系统输入不再生效
  • 或者 Game 窗口焦点丢失,导致键盘事件没传递
  • 又或者 MRTK InputSimulator 没接收到新版 Input System 的按键事件

三、分步排查过程

1. 检查输入系统设置

路径:

复制代码
Edit → Project Settings → Player → Other Settings → Active Input Handling

三种模式:

  • Input Manager (Old):仅旧输入系统;
  • Input System (New):仅新输入系统;
  • Both :两者同时启用。

测试结果:

  • 旧输入系统所有事件均不生效
  • 新输入系统手势模拟正常,仅通过键盘控制Camera的输入不生效

结果:还是不行。手势模拟依旧正常,WASD 仍无反应。至少,现在确定了当前版本的MRTK3必须使用新输入系统


2. 检查 Game 视图焦点与锁定设置

  • Lock Input to Game View:开启后键盘与鼠标输入只传入 Game 窗口;
  • Play Mode Focused:播放时自动聚焦 Game 窗口。

路径如下:

复制代码
Game 视图右上角 ⚙ → Lock Input to Game View
Edit → Preferences → General → Focus Behavior → Play Mode Focused

启用后再次测试:

依旧无效。

这时基本可以确定:

问题不是焦点或窗口,而是输入系统本身。


3. 编写输入诊断脚本

为了判断输入到底有没有被 Unity 捕获,我写了一个小脚本 InputDiagnostics.cs

csharp 复制代码
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif

public class InputDiagnostics : MonoBehaviour
{
    void Update()
    {
        bool oldW = Input.GetKey(KeyCode.W);
        Debug.Log($"OldInput - W:{oldW}");

        #if ENABLE_INPUT_SYSTEM
        var kb = Keyboard.current;
        if (kb != null)
            Debug.Log($"NewInput - W:{kb.wKey.isPressed}");
        #endif
    }
}

运行后在 Console 里发现:

复制代码
OldInput - W:False
NewInput - W:True

这说明:

  • Unity 的新输入系统能正确检测到键盘;
  • 但 MRTKInputSimulator 使用的旧 API 完全收不到输入。

至此,当前估计问题出在InputSystem这里了。


4. 尝试旧系统兼容 + 重新聚焦

再次确认以下设置都正确:

  • Active Input Handling = Both
  • Game 视图锁定输入
  • MRTKInputSimulator 启用状态
  • 没有 UI 输入框截获输入焦点

依然无效。

于是我尝试最后一个临时方案:

直接添加一个手动相机移动脚本(绕开 MRTK)。

csharp 复制代码
using UnityEngine;

public class SimpleCameraController : MonoBehaviour
{
    public float moveSpeed = 2f;

    void Update()
    {
        Vector3 move = Vector3.zero;
        if (Input.GetKey(KeyCode.W)) move += transform.forward;
        if (Input.GetKey(KeyCode.S)) move -= transform.forward;
        if (Input.GetKey(KeyCode.A)) move -= transform.right;
        if (Input.GetKey(KeyCode.D)) move += transform.right;
        transform.position += move * moveSpeed * Time.deltaTime;
    }
}

这段脚本能工作,但只是权宜之计。

我仍然希望恢复 MRTK Input Simulator 的原生行为。


四、最终解决:升级 Input System 包版本

1. 怀疑为InputSystem的Bug

经过所有排查之后,我怀疑问题出在 Unity 自带的 Input System 包实现版本上。

于是打开:

复制代码
Window → Package Manager → Unity Registry → Input System
  • 发现当前版本是 1.14.0(Unity 2022 默认自带的旧版)。
  • 切换到"Version History",发现,最新版本,"Fixed... "

2. 查阅更新日志,现已修复

[1.14.2] - 2025-08-05

txt 复制代码
[1.14.2] - 2025-08-05
Add version 1.14.2 by name.
Fixed
Fixed an issue where using Pen devices on Android tablets would result in double clicks for UI interactions. ISXB-1456
Fixed an issue preventing an embedded platform from being released. It adds back some #defines to XRSupport and InputDeviceCharacteristics.

[1.14.1] - 2025-07-10

txt 复制代码
[1.14.1] - 2025-07-10
Add version 1.14.1 by name.
Added
Support for Xbox controllers over USB on macOS, using macOS's default driver. [ISXB-1548]
Fixed
Fixed an analytics event being invoked twice when the Save button in the Actions view was pressed. ISXB-1378
Fixed an issue causing a number of errors to be displayed when using InputTestFixture in playmode tests with domain reloading disabled on playmode entry. ISXB-1446
Fixed issue where user was not prompted to save changes when loading a second input actions asset into an already opened editor. ISXB-1343
Fixed the on hover behaviour of the two plus buttons in the Input Actions Editor window ISXB-1327
Fixed an issue on macOS which didn't detect up-left DPAD presses for Xbox controllers. ISXB-810
Fixed Input Actions code generation overwriting user files when the names happened to match. ISXB-1257
Fixed Input Actions code generation using locale-dependent rules when lowercasing and uppercasing strings. [ISXB-1406]
Fixed an issue when providing JoinPlayer with a specific split screen index. ISXB-897
Fixed Inspector Window being refreshed all the time when a PlayerInput component is present with Invoke Unity Events nofication mode chosen ISXB-1448
Fixed an issue where an action with a name containing a slash "/" could not be found via InputActionAsset.FindAction(string,bool). ISXB-1306.
Fixed Gamepad stick up/down inputs that were not recognized in WebGL. ISXB-1090
Fixed reenabling the VirtualMouseInput component may sometimes lead to NullReferenceException. ISXB-1096
Fixed the default button press point not being respected in Editor (as well as some other Touchscreen properties). ISXB-1152
Fixed the TreeView compilation warnings when used with Unity 6.2 beta (ISX-2320)
Fixed actions being reset when disabling the InputSystemUIInputModule component ISXB-1493
Fixed a memory leak when disabling and enabling the InputSystemUIInputModule component at runtime ISXB-1573
Fixed all InputControls being changed at once when you change just one by reverting 2a37caac288ac09bc9122234339dc5df8d3a0ca6, which was an attempt at fixing [ISXB-1221] that introduced this regression [ISXB-1531] (https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1493)
Fixed PlayerInput component automatically switching away from the default ActionMap set to 'None'.
Fixed a console error being shown when targeting visionOS builds in 2022.3.
Fixed a Tap Interaction issue with analog controls. The Tap interaction would keep re-starting after timeout. ISXB-627
Fixed the defaultActionMap dropdown in the PlayerInput component defaulting to instead of the first ActionMap.
Fixed TrackedPoseDriver stops updating position and rotation when device is added after its initialization. ISXB-1555
Fixed PlayerInput component not working with C# Wrappers (ISXB-1535). This reverted changes done to fix ISXB-920 but users can now fix it themselves.
Fixed an issue that caused input processors with enum properties to incorrectly serialise by index instead of by value ISXB-1474

3. 更新InputSystem后解决

  • Unity 重新导入后重启项目。

重新运行场景后------

🎉 WASD 完全恢复正常!

  • 手势模拟 -> 正常
  • WASD/QE 移动 相机 -> 正常
  • 鼠标视角旋转 相机 -> 正常

4. 原因剖析

MRTK3 在 InputSimulator 内部逻辑中,会通过 Input System 的 Keyboard API 捕获键盘输入:

csharp 复制代码
Keyboard.current.wKey.isPressed

然而在 Unity 2022 早期 LTS + 旧 Input System 包版本中,
Keyboard.current 存在初始化时机 Bug:

在 Editor Play 模式下可能为 null,导致无法检测按键。

官方修复了这一问题(包括 Editor 模式下 Keyboard.current 注册不稳定的 Bug)。


五、经验分享与优化建议

项目 建议配置
Unity 版本 2022.3.x LTS
MRTK 版本 4.0.0.pre.1(通过MRTK Feature Tool安装)
Input System 建议使用 1.14.2 或更新版本 (2025.08.07发布,当前最新)
Active Input Handling "Both"
Game 视图设置 勾选 "Lock Input to Game View"
Play Mode Focus 选择 "Focused"
XR Plugin OpenXR(建议最新)

如果依然无效,可临时添加 HybridCameraMove 脚本

csharp 复制代码
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
using UnityEngine;

public class HybridCameraMove : MonoBehaviour
{
    public float moveSpeed = 2f;

    void Update()
    {
        Vector3 dir = Vector3.zero;
        #if ENABLE_INPUT_SYSTEM
        var kb = Keyboard.current;
        if (kb != null)
        {
            if (kb.wKey.isPressed) dir += transform.forward;
            if (kb.sKey.isPressed) dir -= transform.forward;
            if (kb.aKey.isPressed) dir -= transform.right;
            if (kb.dKey.isPressed) dir += transform.right;
        }
        #endif
        transform.position += dir * moveSpeed * Time.deltaTime;
    }
}

六、结语

问题根源:

  • Unity 2022 的旧输入兼容层在旧版 Input System 下存在 Bug;
  • MRTKInputSimulator 依赖新输入系统的 Keyboard API;
  • 老版本包在编辑器模式下初始化 Keyboard.current 失败。
    最终解决:

升级 Input System 包(1.14.2),重新导入项目。


这一问题非常具有代表性,反映了 Unity 2022 LTS 向新输入系统全面过渡的"过渡期阵痛"

  • 如果你在使用 MRTK3,务必确保 Input System 是最新;

  • 如果你在升级旧工程,请检查:

    • Input Handling 模式;
    • Input System 包版本;
    • MRTK 的 Input Simulation 服务配置。

最终结论:不是你的配置问题,而是包版本兼容性问题。

一次简单的升级,解决了全部疑难。


相关推荐
双翌视觉16 小时前
机器视觉在手机摄像头模组点胶中的应用
数码相机·智能手机
星夜泊客18 小时前
Unity 游戏开发中的防御性编程与空值处理实践
unity·设计模式·游戏引擎
夏之繁花18 小时前
SODA v9.5.2 甜盐相机,自然美颜相机
数码相机·相机·美颜相机
mit6.8241 天前
[无人机sdk] Open Protocol | 协议包构造&验证
游戏引擎·无人机·cocos2d
tealcwu1 天前
【Unity踩坑】Unity测试用例命名空间错误解决方案
unity·游戏引擎·测试用例
星辰大海14121 天前
摄影入门学习笔记
笔记·数码相机·学习
格林威1 天前
AOI在人形机器人制造领域的应用
人工智能·数码相机·算法·目标跟踪·机器人·视觉检测·制造
AA陈超1 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-28 构建属性菜单小部件控制器
c++·游戏·ue5·游戏引擎·虚幻
地狱为王1 天前
Unity使用PP-MattingV2实现人像分割
unity·游戏引擎