在设置了InputField.shouldHideMobileInput为true之后,安卓端的输入框就回隐藏,直接在InputField中输入。由此也引来了另一个问题:安卓端将输入法最小化后,再点击InputField无法唤出输入法的问题。
此时点击旁边再点击InputField可以重新唤出输入法,但是测试认为这是个bug需要解决,因此来尝试一下。
原来的思路是通过InputField的API在OnPointerClick的时候在此调用一次啊激活选中状态,试了以下几种以及Select和SelectAll等,都无法触发OnSelect。
image.png
后来突然想到,之前做PC端账户密码输入时用过选中输入框的方法,于是通过EventSystem.SetSelectedGameObject终于成功调用到了OnSelect。唯一的缺点是首次点击时会调用两次OnSelect(因为OnPointerClick中又调用了一次)。但输入法最小化后再点击输入框无法唤出输入法的问题算是解决了。代码如下:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 点击输入框拾取输入框,需将脚本拖至InputField上
/// 为解决安卓端将输入法最小化后,再点击InputField无法唤出输入法的问题
/// </summary>
public class ClickSelectInputField : EventTrigger
{
private EventSystem eventSystem;
private InputField inputField;
private void Start()
{
inputField = GetComponent<InputField>();
eventSystem = EventSystem.current;
}
/// <summary>
/// 选中时回调
/// </summary>
/// <param name="eventData"></param>
public override void OnSelect(BaseEventData eventData)
{
Debug.Log("OnSelect:" + inputField.name);
}
/// <summary>
/// 点击时回调
/// </summary>
/// <param name="eventData"></param>
public override void OnPointerClick(PointerEventData eventData)
{
//先丢失选中状态再选中
eventSystem.SetSelectedGameObject(null);
eventSystem.SetSelectedGameObject(inputField.gameObject, new BaseEventData(eventSystem));
}
}
没有细测,发现上面的代码又引起了其他问题:如果已经输入了部分字符,这时候再去点击输入框或者想把光标移动到已输入的字符中间,这时候会是全选的状态。想修改某个字符就很困难了。
搜了很多,貌似大家没有碰到类似的问题,解决代码如下:
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 点击输入框拾取输入框,需将脚本拖至InputField上
/// 为解决安卓端将输入法最小化后,再点击InputField无法唤出输入法的问题
/// </summary>
public class ClickSelectInputField : EventTrigger
{
private EventSystem eventSystem;
private InputField inputField;
private int selectionFocusPosition, selectionAnchorPosition, caretPosition, caretWidth;
private void Start()
{
inputField = GetComponent<InputField>();
eventSystem = EventSystem.current;
caretWidth = inputField.caretWidth;
}
/// <summary>
/// 选中时回调
/// </summary>
/// <param name="eventData"></param>
public override void OnSelect(BaseEventData eventData)
{
Debug.Log("OnSelect:" + inputField.name);
//光标移动到输入框选中位置
StartCoroutine(IEMoveToClickPosition());
}
IEnumerator IEMoveToClickPosition()
{
yield return new WaitForEndOfFrame();
//重新赋值记录过的光标起点终点选中位置
inputField.caretPosition = caretPosition;
inputField.selectionAnchorPosition = selectionAnchorPosition;
inputField.selectionFocusPosition = selectionFocusPosition;
//显示光标
inputField.caretWidth = caretWidth;
}
/// <summary>
/// 点击时回调
/// </summary>
/// <param name="eventData"></param>
public override void OnPointerClick(PointerEventData eventData)
{
//记录光标起点终点选中位置
caretPosition = inputField.caretPosition;
selectionAnchorPosition = inputField.selectionAnchorPosition;
selectionFocusPosition = inputField.selectionFocusPosition;
StartCoroutine(IEReSelectedInputField());
}
/// <summary>
/// 重新选中InputField
/// </summary>
/// <returns></returns>
IEnumerator IEReSelectedInputField()
{
//先丢失选中状态
eventSystem.SetSelectedGameObject(null);
//隐藏光标
inputField.caretWidth = 0;
//等待当前帧数执行完毕,解决安卓端光标闪烁问题
yield return 3;
//再选中状态
eventSystem.SetSelectedGameObject(inputField.gameObject, new BaseEventData(eventSystem));
}
}
点击已输入字符的某个位置可以直接把光标移动过去,点击后拖拽选中也支持~完美
突然发现这个问题在Unity2019.2的版本和.3的版本在移动端的表现还不一样,Unity2019.3.15f的版本中用上述解决方案没问题,但.2的版本中仍然有问题,暂时没有深入调研.2的解决方案。
网友评论