I’m currently working on a game on iPhone with resources regained over time like a few game already do. I was wondering how do you make a countdown that continue while the apps is closed. I’ve read about the System.DateTime.Now that tell you the current real time on the machine but it never seemed to work like I wanted. If someone could give me hints or like me to a similar thread, it would be really appreciated because all I’ve found for the moment is how to make a basic timer…
Thanks for your help!
System.DateTime.Now.ToUniversalTime(); this function returns the current time in a universal format. what you have to do is save it to local file (or playerprefs) and reload it back when you open the app again get the time using the same function. Now is you find the difference between these you will get the time offline, subtract it from the countdown time you have saved early to the local. The difference will be in System.TimeSpan so should know abt tht too
this code i used to retrieve the difference in minutes
double lastMinutes = System.Double.Parse(temp);
System.DateTime dt2 = System.DateTime.Now.ToUniversalTime();
System.TimeSpan dt3 = dt2-((new System.DateTime(0))).ToUniversalTime();
double currentMinutes = dt3.TotalMinutes;
// this code in start..
previously i have saved it from the OnApplicationQuit method of monobehaviour
System.DateTime dt = System.DateTime.Now.ToUniversalTime();
System.DateTime dt1 = new System.DateTime(0).ToUniversalTime();
System.TimeSpan time1 = dt-dt1;
double lastMinutes = time1.TotalMinutes;
im changing double to string because playerprefs doesnt have option to save double,hence saving it as string and retrieving original data on loading
Try to store time from internet source when application closes and on application load get time again. After this make your desired computation for gained resources.
You can extend this idea to hash/crypt time in player preferences along with comparision of time on device to be sure user does not cheat.
If watching time is crucial for your app you must be carefull about not let app fooled by user. i think that online time check is cruial for this.