我知道之前曾有人问过这个问题,但是所有解决方案似乎都无法在移动设备上运行。我发现here的解决方案可以在我的PC上完美运行,但不幸的是,在我的平板电脑上效果不佳。我对解决方案进行了评论,以查看他们是否可以提供帮助,但似乎他们已经一年没有使用SO了。因此,我在这里试试运气,看看是否有人知道如何使其解决方案在移动设备上运行?
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TextFieldBehaviour : MonoBehaviour, ISelectHandler
{
private InputField inputField;
private bool isCaretPositionReset = false;
void Start()
{
inputField = gameObject.GetComponent<InputField>();
}
public void OnSelect(BaseEventData eventData)
{
StartCoroutine(disableHighlight());
}
IEnumerator disableHighlight()
{
Debug.Log("Selected!");
//Get original selection color
Color originalTextColor = inputField.selectionColor;
//Remove alpha
originalTextColor.a = 0f;
//Apply new selection color without alpha
inputField.selectionColor = originalTextColor;
//Wait one Frame(MUST DO THIS!)
yield return null;
//Change the caret pos to the end of the text
inputField.caretPosition = inputField.text.Length;
//Return alpha
originalTextColor.a = 1f;
//Apply new selection color with alpha
inputField.selectionColor = originalTextColor;
if (inputField.touchScreenKeyboard.canSetSelection)
inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);
}
}