CountdownDisplay.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using TMPro;
  3. using UnityEngine;
  4. using Debug = System.Diagnostics.Debug;
  5. namespace Display
  6. {
  7. public class CountdownDisplay : InFrontOfCameraDisplay
  8. {
  9. public int countdown = 3;
  10. public TextMeshProUGUI countdownText;
  11. public delegate string CountdownDoneCallback();
  12. public CountdownDoneCallback OnCountdownDone { get; set; }
  13. private float lastTime;
  14. protected override void Start()
  15. {
  16. base.Start();
  17. lastTime = Time.time;
  18. }
  19. protected override void Update()
  20. {
  21. var t = Time.time;
  22. if (t - lastTime >= 1)
  23. {
  24. lastTime = t;
  25. countdown--;
  26. countdownText.text = $"{countdown}";
  27. if (countdown == 0)
  28. {
  29. countdownText.text = OnCountdownDone?.Invoke();
  30. Destroy(gameObject, 0.5f);
  31. }
  32. }
  33. base.Update();
  34. }
  35. }
  36. }