u3d计算机获取键盘输入,Unity 中的键盘输入

Unity 中的键盘输入Keyboard input in Unity

03/30/2021

本文内容

命名空间: UnityEngineNamespace: UnityEngine

虽然 HoloLens 支持多种形式的输入,包括蓝牙键盘,但大多数应用程序都无法假定所有用户都有可用的物理键盘。While HoloLens supports many forms of input including Bluetooth keyboards, most applications can't assume that all users will have a physical keyboard available. 如果你的应用程序需要文本输入,则应该提供某种形式的屏幕键盘。If your application requires text input, some form of on-screen keyboard should be provided.

Unity 提供了Unity provides the

Unity 中的 HoloLens 系统键盘行为HoloLens system keyboard behavior in Unity

在 HoloLens 上, TouchScreenKeyboard 利用系统的屏幕键盘,并直接覆盖 MR 应用程序的容量耗尽视图。On HoloLens, the TouchScreenKeyboard leverages the system's on-screen keyboard and directly overlays on top of the volumetric view of your MR application. 经验类似于在 HoloLens 内置应用程序中使用键盘。The experience is similar to using keyboard in the built-in apps of HoloLens. 请注意,系统键盘的行为取决于目标平台的功能,例如,HoloLens 2 上的键盘支持直接交互,而 HoloLens 上的键盘 (第一代) 将支持 GGV (注视、手势和语音) 。Note that the system keyboard will behave according to the target platform's capabilities, for example the keyboard on HoloLens 2 would support direct hand interactions, while the keyboard on HoloLens (1st gen) would support GGV (Gaze, Gesture, and Voice). 此外,在从编辑器向 HoloLens 执行 Unity 远程处理时,系统键盘不会显示。Additionally, the system keyboard will not show up when performing Unity Remoting from the editor to a HoloLens.

在 Unity 应用中使用系统键盘Using the system keyboard in your Unity app

声明键盘Declare the keyboard

在类中,声明一个用于存储 TouchScreenKeyboard 的变量,并声明一个用于保存键盘返回的字符串的变量。In the class, declare a variable to store the TouchScreenKeyboard and a variable to hold the string the keyboard returns.

UnityEngine.TouchScreenKeyboard keyboard;

public static string keyboardText = "";

调用键盘Invoke the keyboard

当请求键盘输入的事件发生时,使用以下项显示键盘。When an event occurs requesting keyboard input, use the following to show the keyboard.

keyboard = TouchScreenKeyboard.Open("text to edit");

您可以使用传递到函数的其他参数 TouchScreenKeyboard.Open 来控制键盘的行为 (例如,设置占位符文本或支持自动更正) 。You can use additional parameters passed into the TouchScreenKeyboard.Open function to control the behavior of the keyboard (e.g. setting placeholder text or supporting autocorrection). 有关参数的完整列表,请参阅 Unity 的文档。For the full list of parameters please refer to Unity's documentation.

检索类型化内容Retrieve typed contents

只需调用即可检索内容 keyboard.text 。The content can simply be retrieved by calling keyboard.text. 您可能想要每帧检索内容,或仅在键盘关闭时检索内容。You may want to retrieve the content per frame or only when the keyboard is closed.

keyboardText = keyboard.text;

备用键盘选项Alternative keyboard options

除了直接使用 TouchScreenKeyboard 类之外,还可以使用 Unity 的 UI 输入字段 或 TextMeshPro 输入字段 获取用户输入。Besides using the TouchScreenKeyboard class directly, you can also get user input by using Unity's UI Input Field or TextMeshPro Input Field. 此外,在 MRTK的 HandInteractionExamples 场景中有一个基于 TouchScreenKeyboard 的实现 () 左侧有一个键盘交互示例。Additionally, there is an implementation based on TouchScreenKeyboard in the HandInteractionExamples scene of MRTK (there is a keyboard interaction sample on the left hand side).

下一个开发检查点Next Development Checkpoint

如果遵循我们所说的 Unity 开发旅程,就是探索混合现实平台功能和 Api。If you're following the Unity development journey we've laid out, you're in the midst of exploring the Mixed Reality platform capabilities and APIs. 在这里,你可以继续阅读任何 主题 ,也可以直接跳转到在设备或模拟器上部署你的应用程序。From here, you can continue to any topic or jump directly to deploying your app on a device or emulator.