在 Unity 6 中使用 Nova UI 可滚动输入框时,我们遇到了一个问题:在 Windows、macOS 等桌面平台上,TouchScreenKeyboard.isSupported 会返回 true。
然而虚拟键盘并没有出现。Unity 对“支持”的定义比较宽泛:只要物理键盘可用,就会返回 true,即使并未显示软件键盘。
NovaUI 的 TextFieldMobileInput.InputLoop 中的问题
if (touchScreenKeyboardController.Status != TouchScreenKeyboard.Status.Visible)
这个检查会不断错误地假定键盘已经显示,导致意外丢失焦点或协程中断。
正确的解决方法
不要只依赖 TouchScreenKeyboard.isSupported。结合 Application.isMobilePlatform,明确检查运行时平台:
void Awake()
{
if (!Application.isMobilePlatform)
{
Debug.Log("TextFieldMobileInput disabled on non-mobile platform.");
this.enabled = false;
}
}
也可以调整 NovaUI 中的 TextFieldMobileInput 组件:
protected override void OnEnable()
{
Debug.Log("TouchScreenKeyboard.isSupported: " + TouchScreenKeyboard.isSupported);
if (!Application.isMobilePlatform || !TouchScreenKeyboard.isSupported)
{
Debug.Log("Disabling TextFieldMobileInput – not a mobile platform");
enabled = false;
// If touchscreen keyboard not supported on this device, do nothing
return;
}
base.OnEnable();
// Subscribe to selection changed so we can update the touch screen
// keyboard whenever the input field selection changes
inputField.OnTextSelectionChanged += UpdateTouchScreenKeyboardSelection;
}
这样就不会在不支持移动输入的平台上执行相应代码。
结果
- 在 Android/iOS 上:
TextFieldMobileInput处理软件键盘。 - 在桌面端:
TextFieldKeyboardInput正常接管输入。 - 不再出现错误的键盘检查或不必要的协程。
额外建议
也可以通过上下文菜单或验证脚本,在 Editor 中执行检查,避免 prefab 在测试时意外重新启用该组件。
结语
Unity 的平台差异又一次带来了问题,不过一行防护检查就能让 Nova UI 输入在各平台上正确运行。

Unity Nova UI:在非移动平台上,对运行时 TouchScreenKeyboard 逻辑进行防护。
