rabidus-test/Assets/Scripts/UITextShow.cs

113 lines
2.9 KiB
C#
Raw 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;
2023-10-09 16:51:27 +03:00
using UnityEngine.Events;
2023-08-15 17:38:54 +03:00
public class UITextShow : MonoBehaviour
{
private TMPro.TextMeshProUGUI _text;
2023-10-02 19:12:35 +03:00
private Coroutine coroutine;
2023-10-09 16:51:27 +03:00
public UnityEvent OnTimeTick;
public UnityEvent OnTimeEnd;
2023-10-11 17:33:14 +03:00
public string Prefix;
public string Postfix;
2023-10-11 15:44:05 +03:00
public string Text => _text.text;
2023-08-15 17:38:54 +03:00
private void Awake()
{
_text = GetComponent<TMPro.TextMeshProUGUI>();
}
public void SetText(string text)
{
2023-10-11 17:33:14 +03:00
_text.SetText($"{Prefix}{text}{Postfix}");
2023-10-27 17:29:01 +03:00
_text.SetAllDirty();
2023-10-11 17:33:14 +03:00
}
public void SetText(int num)
{
SetText(num.ToString());
2023-08-15 17:38:54 +03:00
}
public void SetText(string text, float afterDelay, Action onStart, Action onEnd)
{
if (coroutine != null)
{
StopCoroutine(coroutine);
}
coroutine = StartCoroutine(SetText_Coroutine(text, afterDelay, onStart, onEnd));
}
2023-08-15 17:38:54 +03:00
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--)
{
2023-10-11 17:33:14 +03:00
SetText(i.ToString());
2023-10-09 16:51:27 +03:00
OnTimeTick?.Invoke();
2023-08-15 17:38:54 +03:00
yield return new WaitForSeconds(1);
}
2023-10-11 17:33:14 +03:00
SetText(afterTimerText);
2023-08-15 17:38:54 +03:00
yield return new WaitForSeconds(afterDelay);
2023-10-11 11:58:06 +03:00
//_text.SetText(string.Empty);
2023-10-09 16:51:27 +03:00
OnTimeEnd?.Invoke();
2023-08-15 17:38:54 +03:00
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]);
2023-10-11 17:33:14 +03:00
SetText(sb.ToString());
OnTimeTick?.Invoke();
2023-08-15 17:38:54 +03:00
yield return new WaitForSeconds(delay);
}
yield return new WaitForSeconds(afterDelay);
2023-10-11 17:33:14 +03:00
SetText(string.Empty);
OnTimeEnd?.Invoke();
2023-08-15 17:38:54 +03:00
onEnd?.Invoke();
}
private IEnumerator SetText_Coroutine(string text, float afterDelay, Action onStart, Action onEnd)
{
onStart?.Invoke();
SetText(text);
yield return new WaitForSeconds(afterDelay);
SetText(string.Empty);
OnTimeEnd?.Invoke();
onEnd?.Invoke();
}
2023-08-15 17:38:54 +03:00
}