Real Time(taking actual present system time) Countdown Timer

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;

public class NewTimer : MonoBehaviour {

public Text timerText; // Text that holds the time
public float msToWait = 5000.0f; // Time in milliseconds
private ulong TimerTarget;
int buttoncounter = 0; // counting for clicks of button

void Start () {
TimerTarget = ulong.Parse(PlayerPrefs.GetString("LastChestOpen"));

timerText = GetComponent<Text>();
}

// Update is called once per frame
void Update () {

//set timer
ulong diff = ((ulong) DateTime.Now.Ticks - TimerTarget);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float secondsLeft = (float)(msToWait - m) / 1000.0f;

string r = "";

//Hours
r+= ((int) secondsLeft / 3600).ToString() + "H : ";
secondsLeft -= ((int)secondsLeft / 3600) * 3600;

//minutes
r+= ((int) secondsLeft / 60).ToString("00") + "M : ";

//seconds
r+= (secondsLeft % 60).ToString("00") + "S ";
timerText.text = r;

if (timerText == null) {

Debug.Log ("No text is attached");
}

if (secondsLeft < 0) // todo: when time is up!
{
//do something.
timerText.text = "Time"; // change text if we want
}
}

public void ClickToStartTimer() // for button to activate to set present time.
{
  // Action here and set string for player prefs with value to the text (Timertext)
// set the timer target to parse ulong to datetime.now
}
}

private bool IsTickingTimer() // checks if timer is ticking, if yes,timer is --. if no, time ++
{
ulong diff = ((ulong)DateTime.Now.Ticks - TimerTarget);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float secondsLeft = (float)(msToWait - m) / 1000.0f;

}
}

Comments

Popular posts from this blog