CountdownDisplay.cs 970 B

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