Hiệu ứng chữ chạy (Typewriter Effect)
C# Code
using System.Collections;
using UnityEngine;
using TMPro;
public class TypewriterEffect : MonoBehaviour
{
public TextMeshProUGUI textComponent;
public float delay = 0.05f;
private string fullText;
void Awake()
{
if (textComponent == null) textComponent = GetComponent<TextMeshProUGUI>();
fullText = textComponent.text;
textComponent.text = "";
}
void Start()
{
StartCoroutine(ShowText());
}
IEnumerator ShowText()
{
for (int i = 0; i <= fullText.Length; i++)
{
textComponent.text = fullText.Substring(0, i);
yield return new WaitForSeconds(delay);
}
}
}Làm cho văn bản trong TextMeshPro xuất hiện tuần tự từng chữ một, tạo cảm giác như đang được gõ máy chữ, thường dùng trong hội thoại.