Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class Timer : MonoBehaviour
- {
- [SerializeField] private TextMeshProUGUI TimerText;
- private bool m_TrackingTime = false;
- private float m_ElapsedTime;
- private void OnEnable()
- {
- ClearDisplay();
- Countdown.CountdownComplete += StartTimer;
- DragStripLogic.onCarHasReachedEndDelegate += StopTimer;
- }
- private void OnDisable()
- {
- Countdown.CountdownComplete -= StartTimer;
- DragStripLogic.onCarHasReachedEndDelegate -= StopTimer;
- }
- void StartTimer()
- {
- m_TrackingTime = true;
- }
- void StopTimer()
- {
- m_TrackingTime = false;
- }
- private void Update()
- {
- if (!m_TrackingTime) return;
- m_ElapsedTime += Time.deltaTime;
- UpdateDisplay();
- }
- void ClearDisplay()
- {
- TimerText.SetText("");
- }
- void UpdateDisplay()
- {
- TimerText.SetText(string.Format("{0}s", m_ElapsedTime.ToString("F2")));
- }
- public float GetRecordedTime()
- {
- return m_ElapsedTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement