unity项目发布为APK后,在安卓手机上所有输入框不显示光标
此文章是为了记录通过逻辑实时绘制一个假的光标
只适用于InputField,不用于TMP_InputField,因为逻辑中用到了GetGenerationSettings的方法,TMP_InputField并没有此方法,此次就没有再进一步研究兼容的方法
第一步,将一个假的光标,放在text下面,锚点按照下图设置,加个tween动画就能一闪闪了
using UnityEngine;
using UnityEngine.UI;
public class CustomCaret : MonoBehaviour
{
public InputField inputField;
public RectTransform caretImage;
private void Start()
{
if (caretImage)
{
caretImage.sizeDelta = new Vector2(2, inputField.textComponent.fontSize);
}
}
private void Update()
{
if (inputField.isFocused)
{
UpdateCaretPosition();
caretImage.gameObject.SetActive(true);
}
else
{
caretImage.gameObject.SetActive(false);
}
}
private void UpdateCaretPosition()
{
//int caretPosition = inputField.caretPosition;
//Text textComponent = inputField.textComponent;
//string text = inputField.text;
//if (caretPosition > text.Length)
//{
// caretPosition = text.Length;
//}
//Vector2 caretPos = textComponent.rectTransform.anchoredPosition;
//if (caretPosition > 0)
//{
// TextGenerator textGen = new TextGenerator(caretPosition);
// TextGenerationSettings settings = textComponent.GetGenerationSettings(textComponent.rectTransform.rect.size);
// float width = textGen.GetPreferredWidth(text.Substring(0, caretPosition), settings);
// caretPos.x += width;
//}
//caretImage.anchoredPosition = caretPos;
//int caretPosition = inputField.caretPosition;
//Text textComponent = inputField.textComponent;
//string text = inputField.text;
Generate text layout up to the caret position
//TextGenerator textGen = new TextGenerator();
//TextGenerationSettings settings = textComponent.GetGenerationSettings(textComponent.rectTransform.rect.size);
//textGen.Populate(text.Substring(0, caretPosition), settings);
Calculate the position of the caret
//Vector2 caretPos = textComponent.rectTransform.anchoredPosition;
//if (textGen.characterCount > 0)
//{
// UICharInfo charInfo = textGen.characters[textGen.characterCount - 1];
// caretPos.x += charInfo.cursorPos.x;
// caretPos.y += charInfo.cursorPos.y;
//}
//caretImage.anchoredPosition = caretPos;
//int caretPosition = inputField.caretPosition;
//Text textComponent = inputField.textComponent;
//string text = inputField.text;
Generate text layout up to the caret position
//TextGenerator textGen = new TextGenerator();
//TextGenerationSettings settings = textComponent.GetGenerationSettings(textComponent.rectTransform.rect.size);
//textGen.Populate(text, settings);
Calculate the position of the caret
//Vector2 caretPos = Vector2.zero;
//float totalWidth = 0;
//for (int i = 0; i < caretPosition; i++)
//{
// if (i < textGen.characterCount)
// {
// totalWidth += textGen.characters[i].charWidth;
// }
//}
//caretPos.x = totalWidth;
//caretImage.anchoredPosition = caretPos;
int caretPosition = inputField.caretPosition;
Text textComponent = inputField.textComponent;
string text = inputField.text;
// Calculate the width of the text up to the caret position
TextGenerator textGen = new TextGenerator();
TextGenerationSettings settings = textComponent.GetGenerationSettings(textComponent.rectTransform.rect.size);
/// settings.scaleFactor 除以此字段,用于适配不同分辨率
float width = textGen.GetPreferredWidth(text.Substring(0, caretPosition), settings) / settings.scaleFactor;
// Set the position of the caret
Vector2 caretPos = textComponent.rectTransform.anchoredPosition;
caretPos.x += width;
caretImage.anchoredPosition = caretPos;
}
}