在HoloLens中使用UGUI InputField组件时,存在一个问题。当点击一次InputField时,将弹出的键盘关掉,再点击其它地方,又会因InputField再次获得焦点而弹出键盘。
解决方法:
将InputField的interactable字段设置为false。添加PointerHandler脚本来响应点击事件,然后另外写脚本来调出输入键盘。
TouchScreenKeyboard.Open("",TouchScreenKeyboardType.ASCIICapable);//打开系统键盘
TTouchScreenKeyboard.hideInput字段在HoloLens中不起作用。
示例:自己调用输入键盘
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Microsoft.MixedReality.Toolkit.Input;
[RequireComponent(typeof(PointerHandler))]
public class InputFieldKeyboard : MonoBehaviour
{
[SerializeField]
private InputField m_InputField;
[SerializeField]
private TouchScreenKeyboardType keyboardType = TouchScreenKeyboardType.Default;
private TouchScreenKeyboard m_Keyboard;
private void Awake()
{
if (m_InputField == null)
m_InputField = this.GetComponent<InputField>();
#if UNITY_EDITOR
m_InputField.interactable = true;
#else
m_InputField.interactable = false;
#endif
}
#if !UNITY_EDITOR
void Update()
{
if (m_Keyboard == null)
return;
switch(m_Keyboard.status)
{
case TouchScreenKeyboard.Status.Visible:
m_InputField.text = m_Keyboard.text;
break;
default:
m_Keyboard = null;
break;
}
}
#endif
public void Open()
{
#if !UNITY_EDITOR
m_InputField.caretPosition = m_InputField.text == null ? 0 : m_InputField.text.Length;
TouchScreenKeyboard.hideInput = false;
m_Keyboard = TouchScreenKeyboard.Open(m_InputField.text, keyboardType);
#endif
}
}