Advertisement
jyourman

Untitled

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