在大多数情况下,Unity 将自动处理 GUI 元素的键盘输入,但也很容易通过脚本按需显示键盘。
当用户点击可编辑的 GUI 元素时,键盘将自动显示。目前,GUI.TextField、GUI.TextArea 和 GUI.PasswordField 将显示键盘;有关更多详细信息,请参阅 GUI 类文档。
使用 TouchScreenKeyboard.Open() 函数打开键盘。有关此函数所用的参数,请参阅 TouchScreenKeyboard 脚本参考。
键盘支持以下选项:
属性: | 功能: |
---|---|
TouchScreenKeyboardType.Default | 字母键盘。可切换到具有数字和标点符号的键盘。 |
TouchScreenKeyboardType.ASCIICapable | 字母键盘。可切换到具有数字和标点符号的键盘。 |
TouchScreenKeyboardType.NumbersAndPunctuation | 数字和标点符号键盘。可切换到具有字母的键盘。 |
TouchScreenKeyboardType.URL | 带斜线和 .com 按钮的字母键盘。可切换到具有数字和标点符号的键盘。 |
TouchScreenKeyboardType.NumberPad | 只有 0 到 9 之间的数字键盘。 |
TouchScreenKeyboardType.PhonePad | 用于输入电话号码的键盘。 |
TouchScreenKeyboardType.NamePhonePad | 字母键盘。可切换到电话号码键盘。 |
TouchScreenKeyboardType.EmailAddress | 具有 @ 符号的字母键盘。可切换到具有数字和标点符号的键盘。 |
默认情况下将创建一个编辑框,并在出现后放置在键盘顶部。此编辑框用于预览用户正在键入的文本,因此文本始终对用户可见。但是,可通过将 TouchScreenKeyboard.hideInput 设置为 true 来禁用文本预览。请注意,此设置仅适用于某些键盘类型和输入模式。例如,不适用于电话键盘和多行文本输入。在此类情况下将始终显示编辑框。TouchScreenKeyboard.hideInput 是一个全局变量,会影响所有键盘。
TouchScreenKeyboard 中有三个键盘属性可用于确定屏幕上的键盘可见性状态和大小。
属性: | 功能: |
---|---|
visible | 如果键盘在屏幕上完全可见并可用于输入字符,则返回 true。 |
area | 返回键盘的位置和尺寸。 |
active | 如果已激活键盘,则返回 true。此属性不是静态属性。必须具有键盘实例才能使用此属性。 |
请注意,__TouchScreenKeyboard.area__ 将返回一个矩形,该矩形的位置和大小设置为 0,直到键盘在屏幕上完全可见。不应在 TouchScreenKeyboard.Open() 之后立即查询此值。键盘事件的顺序如下:
可配置键盘以使其在输入时隐藏符号。当用户需要输入敏感信息(例如密码)时,此功能非常有用。要在启用安全文本输入的情况下手动打开键盘,请使用以下代码:
TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, true);
要显示带有黑色半透明背景而不是经典不透明背景的键盘,请调用 TouchScreenKeyboard.Open(),如下所示:
TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, true, true);
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.