Advertisement
Guest User

Timer

a guest
Aug 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class Timer : MonoBehaviour
  7. {
  8. [SerializeField] private TextMeshProUGUI TimerText;
  9. private bool m_TrackingTime = false;
  10. private float m_ElapsedTime;
  11.  
  12.  
  13.  
  14.  
  15.  
  16. private void OnEnable()
  17. {
  18. ClearDisplay();
  19. Countdown.CountdownComplete += StartTimer;
  20. DragStripLogic.onCarHasReachedEndDelegate += StopTimer;
  21. }
  22.  
  23. private void OnDisable()
  24. {
  25. Countdown.CountdownComplete -= StartTimer;
  26. DragStripLogic.onCarHasReachedEndDelegate -= StopTimer;
  27. }
  28.  
  29.  
  30.  
  31. void StartTimer()
  32. {
  33. m_TrackingTime = true;
  34.  
  35. }
  36.  
  37. void StopTimer()
  38. {
  39. m_TrackingTime = false;
  40. }
  41.  
  42. private void Update()
  43. {
  44.  
  45. if (!m_TrackingTime) return;
  46.  
  47. m_ElapsedTime += Time.deltaTime;
  48. UpdateDisplay();
  49. }
  50.  
  51. void ClearDisplay()
  52. {
  53. TimerText.SetText("");
  54. }
  55.  
  56. void UpdateDisplay()
  57. {
  58. TimerText.SetText(string.Format("{0}s", m_ElapsedTime.ToString("F2")));
  59. }
  60.  
  61. public float GetRecordedTime()
  62. {
  63. return m_ElapsedTime;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement