rabidus-test/Assets/Scripts/UITextShow.cs

74 lines
1.9 KiB
C#
Raw Permalink Normal View History

2023-08-15 17:38:54 +03:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
public class UITextShow : MonoBehaviour
{
private TMPro.TextMeshProUGUI _text;
2023-10-02 19:12:35 +03:00
private Coroutine coroutine;
2023-08-15 17:38:54 +03:00
private void Awake()
{
_text = GetComponent<TMPro.TextMeshProUGUI>();
}
public void SetText(string text)
{
_text.SetText(text);
}
public void ShowSubtitle(string text, float delay, float afterDelay, Action onStart, Action onEnd)
{
2023-10-02 19:12:35 +03:00
if (coroutine != null)
{
StopCoroutine(coroutine);
}
coroutine = StartCoroutine(Subtitle_Coroutine(text, delay, afterDelay, onStart, onEnd));
2023-08-15 17:38:54 +03:00
}
public void ShowTimer(int time, string afterTimerText, float afterDelay, Action onStart, Action onEnd)
{
2023-10-02 19:12:35 +03:00
if (coroutine != null)
{
StopCoroutine(coroutine);
}
2023-08-15 17:38:54 +03:00
StartCoroutine(Timer_Coroutine(time, afterTimerText, afterDelay, onStart, onEnd));
}
private IEnumerator Timer_Coroutine(int time, string afterTimerText, float afterDelay, Action onStart, Action onEnd)
{
onStart?.Invoke();
for (int i = time; i > 0; i--)
{
_text.SetText(i.ToString());
yield return new WaitForSeconds(1);
}
_text.SetText(afterTimerText);
yield return new WaitForSeconds(afterDelay);
_text.SetText(string.Empty);
onEnd?.Invoke();
}
private IEnumerator Subtitle_Coroutine(string text, float delay, float afterDelay, Action onStart, Action onEnd)
{
onStart?.Invoke();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
sb.Append(text[i]);
_text.SetText(sb);
yield return new WaitForSeconds(delay);
}
yield return new WaitForSeconds(afterDelay);
_text.SetText(string.Empty);
onEnd?.Invoke();
}
}