【发布时间】:2018-02-04 10:10:21
【问题描述】:
我知道如何使用InputMethodManager 隐藏或显示虚拟键盘。
但我想使用物理键盘在EditText 中输入文本,但我不想在 Unity 3D Android 中显示虚拟键盘。
如何在 Unity 3D 中做到这一点?
【问题讨论】:
我知道如何使用InputMethodManager 隐藏或显示虚拟键盘。
但我想使用物理键盘在EditText 中输入文本,但我不想在 Unity 3D Android 中显示虚拟键盘。
如何在 Unity 3D 中做到这一点?
【问题讨论】:
Unity 中没有 EditText 这样的东西。 InputField 用于接收来自设备的输入。
您可以在 Android 上使用 InputField 禁用虚拟关键字。不确定这是否适用于其他平台。
你的输入框:
public InputField inputField;
禁用虚拟键盘:
inputField.keyboardType = (TouchScreenKeyboardType)(-1);
启用虚拟键盘:
inputField.keyboardType = TouchScreenKeyboardType.Default;
如果您遇到奇怪的问题,请考虑从InputField 派生脚本,然后禁用虚拟键盘,最后调用InputField 的基本Start 函数:
public class HideVirtualKeyboard : InputField
{
protected override void Start()
{
keyboardType = (TouchScreenKeyboardType)(-1);
base.Start();
}
}
【讨论】:
我知道的老问题,但你真正想要的是设置inputField.touchScreenKeyboard.active = false; 你可能需要在LateUpdate 中保持它为假
[SerializeField]
private bool keyboardEnabled = true;
// toggle this to toggle native keyboard activity
public bool KeyboardEnabled { get { return keyboardEnabled; } set { keyboardEnabled = value; } }
private void LateUpdate()
{
#if UNITY_ANDROID
if (inputField.touchScreenKeyboard != null && !keyboardEnabled)
{
inputField.touchScreenKeyboard.active = false;
}
#endif
}
【讨论】:
Application.onBeforeRender: var ev = EventSystem.current; if (ev != null) { var go = ev.currentSelectedGameObject; if (go != null) { var inp = go.GetComponent<InputField>(); if (inp != null && inp.touchScreenKeyboard != null) inp.touchScreenKeyboard.active = false; } }