Skip to content

Instantly share code, notes, and snippets.

@arun02139
Last active August 23, 2016 06:04
Show Gist options
  • Save arun02139/3d5792d279262399b79db46e2b3020b1 to your computer and use it in GitHub Desktop.
Save arun02139/3d5792d279262399b79db46e2b3020b1 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
using System;
using UnityEngine.Networking;
using Random = UnityEngine.Random;
public class GameTimeUI : MonoBehaviour
{
Text _time;
TimeSpan _timeSpan;
void Awake ()
{
// Debug.Log (string.Format ("GameTimeUI.Awake: {0}", ""));
_time = gameObject.FindRecursive ("Time").GetComponent<Text> ();
}
void Start()
{
StartCoroutine (HookListeners());
}
IEnumerator HookListeners()
{
while (NetworkTimeManager.i == null) {
yield return new WaitForEndOfFrame ();
}
// NOTE, CONFIRM: unity events maintain a weak reference to their subscribers,
// so we don't need to worry about explicitly removing the listener elsewhere
NetworkTimeManager.i.UEventColorFlip.AddListener(ChangeColor);
}
void Update ()
{
if (NetworkTimeManager.i == null)
{
var debugString = string.Format ("GameTimeUI.Update: {0} ({1})",
"NetworkTimeManager.i is null, aborting", NetworkTimeManager.i.secondsRemaining);
ClientDebugUI.i.Log (debugString, GameColors.Yellow);
Debug.LogWarning (debugString);
return;
}
UpdateTimeLeft();
}
void UpdateTimeLeft()
{
var sec = NetworkTimeManager.i.secondsRemaining;
// the line below will only be valid for the host
// if(NetworkServer.active) ..
// LINK: http://stackoverflow.com/questions/463642/what-is-the-best-way-to-convert-seconds-into-hourminutessecondsmilliseconds
_timeSpan = TimeSpan.FromSeconds (sec);
_time.text = string.Format ("{0:D2}:{1:D2}", _timeSpan.Minutes, _timeSpan.Seconds);
// Debug.Log (string.Format ("sec = {0}, _timeSpan.Minutes = {1}, _timeSpan.Seconds = {2}", sec, _timeSpan.Minutes, _timeSpan.Seconds));
}
void ChangeColor(int colorIndex)
{
ClientDebugUI.i.Log (string.Format("GameTimeUI.ChangeColor: {0}", colorIndex));
GetComponent<Image> ().color = Constants.COLORS[colorIndex];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment