unity 绘制输入框光标

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;
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值