I want to create a timer that runs at all times even when the app is closed. I want a timer that can do this so that after 30 minutes a life is replenished. I also want to display this timer with GUI. How can I do this? I know how to create a timer with some code that I will post here, but I do not know how to run it at all times. Also with this code when I call CreateClock() for some reason the clock does not show. Is there a better way to do this? Code (csharp): using UnityEngine; using System.Collections; public class ClockScript : MonoBehaviour { public bool clockIsPaused; public float startTime; //(in seconds) public float timeRemaining; //(in seconds) public bool timeIsUp; public string timeStr; //Screen sizes public int sixthOfScreenW; public int sixthOfScreenH; public GUIStyle labelStyle; public bool displayed; private bool start; public void CreateClock (){ sixthOfScreenW = Screen.width / 6; sixthOfScreenH = Screen.height / 6; clockIsPaused = false; timeIsUp = false; startTime = Time.time + 61.0f; displayed = true; start = true; } void OnGUI (){ if (displayed) { labelStyle.fontSize = Screen.width / 20; GUI.Label ( new Rect(sixthOfScreenW * 4, sixthOfScreenH * 0.5f, sixthOfScreenW, sixthOfScreenH * 0.5f), timeStr, labelStyle); } } void Update (){ if (start) { if (!clockIsPaused) { // make sure the timer is not paused DoCountdown(); } } } void DoCountdown (){ timeRemaining = startTime - Time.time; if (timeRemaining < 0) { timeRemaining = 0; clockIsPaused = true; TimeIsUp(); } ShowTime(); } public void PauseClock (){ clockIsPaused = true; } public void UnpauseClock (){ clockIsPaused = false; } void ShowTime (){ int minutes; int seconds; minutes = (int)timeRemaining/60; seconds = (int)timeRemaining%60; timeStr = "Time: " + minutes.ToString() + ":"; timeStr += seconds.ToString("D2"); } void TimeIsUp (){ timeIsUp = true; } }
You should not run it all the times. Just save current data and time when app is closing, and restore it when app runs again. After this you will get the difference between them and strarts the timer since this time.
That would work but I want a timer like in candy crush saga where once the timer is up there is a push notification that says "Your lives were refreshed!"
The way those notifications work is they are registered while the game is playing. Example (not correct code) Code (csharp): void OnApplicationPause (bool pauseStatus) { if (pauseStatus == true) { NotificationManager.RegisterNotification ("This is a Notification", GetTimeUntilLivesRefreshed ()); } }
I could just use Patico's way but does anyone know how to it like how I stated above with Candy Crush?
Seeing as how Candy crush can be played without any connection to the servers, I don't see how this could be the case. Pretty sure it uses your system time to determine things like this. Save the start time, add thirty minutes, and compare current time to the new value to get your timer. This will work even when you exit the program and resume some time later on. I don't work in mobile, so I'm not sure how push notifications are handled.
So would I use System.DateTime.Now and set the value to a variable. Then the next time the user opens the app retrieve the new System.DateTime.Now value? If so how would I see if the new value is 30 minutes after the old value?