SHARE
TWEET

Untitled

jyourman Aug 27th, 2015 146 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class TimerController : MonoBehaviour
  6. {
  7.         private float _levelDuration; //you'd set this at the beginning of each level
  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.                         //do stuff here to trigger the end of the level
  61.                         //best to probably set a private variable in this class that can be read from another class via a method
  62.                 }
  63.         }
  64.        
  65.         private void SetTimerString(float time)
  66.         {
  67.                 //convert the time to the format you want to show - I'm not doing any formatting :P
  68.                
  69.                 timerText.text = time.ToString();
  70.         }
  71.        
  72.         public void AddToTimer(float time)
  73.         {
  74.                 //either add to levelDuration or deduct from startTime doesn't matter - I choose deducting from the startTime because you might want to reference levelDUration later
  75.                 startTime += time;
  76.         }
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top