unity 实现TMP_Text文本末尾光标闪烁,不使用Tmp_inputfield
时间: 2023-12-14 14:39:17 浏览: 145
实现TMP_Text文本末尾光标闪烁可以通过在Unity中编写脚本来实现。
首先,在Unity中创建一个空对象,将TMP_Text组件添加到该对象中。
然后,创建一个新的C#脚本并将其添加到该对象中。在脚本中,我们需要使用TMP_Text组件和协程来实现闪烁光标。
以下是代码示例:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class BlinkingCursor : MonoBehaviour
{
private TMP_Text textComponent;
void Start()
{
textComponent = GetComponent<TMP_Text>();
StartCoroutine(Blinking());
}
IEnumerator Blinking()
{
while (true)
{
textComponent.text += "|";
yield return new WaitForSeconds(0.5f);
textComponent.text = textComponent.text.Substring(0, textComponent.text.Length - 1);
yield return new WaitForSeconds(0.5f);
}
}
}
```
在上述代码中,我们首先获取TMP_Text组件,并在Start方法中启动协程。协程将在无限循环中运行,并将在文本末尾添加“|”字符来表示光标。然后等待0.5秒钟,然后将字符串中的最后一个字符删除,以达到闪烁的效果。最后,我们再等待0.5秒钟继续循环。
将该脚本添加到TMP_Text组件所在的游戏对象中,即可实现文本末尾光标的闪烁效果。
阅读全文
相关推荐


















