Unity中如何通过代码让Text (TMP)中的文字出现是打字机效果
时间: 2024-02-28 08:53:55 浏览: 239
可以通过以下代码实现Text (TMP)中文字打字机效果:
```C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class TypewriterEffect : MonoBehaviour
{
public TMP_Text textMeshPro;
public float delay = 0.1f;
private string fullText;
private string currentText = "";
void Start()
{
fullText = textMeshPro.text;
textMeshPro.text = "";
StartCoroutine(ShowText());
}
IEnumerator ShowText()
{
for (int i = 0; i <= fullText.Length; i++)
{
currentText = fullText.Substring(0, i);
textMeshPro.text = currentText;
yield return new WaitForSeconds(delay);
}
}
}
```
将以上代码添加到一个脚本中,将TMP_Text组件和延迟时间设置为所需的值,然后将脚本挂载到需要打字机效果的游戏对象上,即可实现该效果。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![cs](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)