Global Game Jam 2021
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.3 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using Variables;
  6. public class StartScreenUI : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private Reference<float> m_StartTime;
  10. [SerializeField]
  11. private TextMeshProUGUI m_countdownText;
  12. [SerializeField]
  13. private AnimationCurve m_lerpCurve;
  14. [SerializeField]
  15. private Transform m_startPosition;
  16. [SerializeField]
  17. private Transform m_endPosition;
  18. private void OnEnable()
  19. {
  20. m_StartTime.OnValueChanged += UpdateCountDown;
  21. }
  22. private void OnDisable()
  23. {
  24. m_StartTime.OnValueChanged -= UpdateCountDown;
  25. }
  26. private void UpdateCountDown(float value)
  27. {
  28. m_countdownText.text = ((int)value).ToString();
  29. float ratio = m_lerpCurve.Evaluate(value % 1);
  30. m_countdownText.transform.position = Vector3.Lerp(m_endPosition.position, m_startPosition.position, ratio);
  31. }
  32. [ContextMenu("Do Count Down")]
  33. private void CountDown()
  34. {
  35. StartCoroutine(CountToZero(m_StartTime));
  36. }
  37. private IEnumerator CountToZero(float start)
  38. {
  39. m_StartTime.Value = start;
  40. while (m_StartTime > 0)
  41. {
  42. m_StartTime.Value -= Time.deltaTime;
  43. yield return new WaitForEndOfFrame();
  44. }
  45. }
  46. }