Car/Assets/Scripts/TMPTypewriter.cs

47 lines
1.1 KiB
C#

using System.Collections;
using TMPro;
using UnityEngine;
using YogiGameCore.Utils.MonoExtent;
[RequireComponent(typeof(TextMeshProUGUI))]
public class TMPTypewriter : MonoBehaviour
{
private TextMeshProUGUI _text;
public float animTotalTime = 1;
private void Awake()
{
_text = GetComponent<TextMeshProUGUI>();
}
[Button]
public void PlayAnimation(string display)
{
StopAllCoroutines();
_text.text = display;
StartCoroutine(TypeWriteCoroutine());
}
private IEnumerator TypeWriteCoroutine()
{
_text.ForceMeshUpdate();
var tmpTextInfo = _text.textInfo;
int total = tmpTextInfo.characterCount;
bool complete = false;
int current = 0;
while (!complete)
{
if (current > total)
{
current = total;
yield return new WaitForSecondsRealtime(0.1f);
complete = true;
}
_text.maxVisibleCharacters = current;
current += 1;
yield return new WaitForSecondsRealtime(animTotalTime / total);
}
yield return null;
}
}