Advertisement
jyourman

Untitled

Aug 29th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class TimerController : MonoBehaviour
  6. {
  7. private float _levelDuration;
  8.  
  9. private Text timerText;
  10. private float startTime;
  11. private float currentTime;
  12. private bool timerActive;
  13.  
  14. void Awake()
  15. {
  16. timerText = gameObject.GetComponent<Text>();
  17. if (timerText == null)
  18. {
  19. Debug.LogError("This script needs to be attached to a GUI text object!");
  20. this.enabled = false;
  21. return;
  22. }
  23. }
  24.  
  25. void Update()
  26. {
  27. UpdateTimer();
  28. }
  29.  
  30. public void SetTimer(float levelDuration)
  31. {
  32. _levelDuration = levelDuration;
  33. SetTimerString(levelDuration);
  34. }
  35.  
  36. public void StartTimer()
  37. {
  38. timerActive = true;
  39. startTime = Time.time;
  40. }
  41.  
  42. public void SetTimerActive(bool active)
  43. {
  44. timerActive = active;
  45. }
  46.  
  47. void UpdateTimer()
  48. {
  49. if (!timerActive)
  50. return;
  51.  
  52. float remainingTime = _levelDuration - (Time.time - startTime);
  53. if (remainingTime > 0.0f)
  54. {
  55. SetTimerString(remainingTime);
  56. }
  57. else
  58. {
  59. SetTimerString(0f);
  60. Application.LoadLevel("GameOver");
  61. }
  62. }
  63.  
  64. private void SetTimerString(float time)
  65. {
  66. timerText.text = time.ToString();
  67. }
  68.  
  69. public void AddToTimer(float time)
  70. {
  71. startTime += time;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement