I’m implementing player energy that regenerates over time for a mobile game. I’m using DateTime to calculate the real time elapsed even when the game is closed, and I’m checking in Update to see if enough time has passed to add energy.
It would probably be more optimal to use a timer that increments the energy every few minutes. How can I do such a thing in Unity, if possible? Being able to query the timer for elapsed/remaining time would be nice.
It looks like youre relying on local information too much… If you want to make it simple and dont care about people cheating then just save a file to disk using something like this … `ProfileClassType ProfileClassTypeInstance;
BinaryFormatter binform = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + “/LocalProfile.dat”);
binform.Serialize (file, PofileClassTypeInstance);
file.Close ();`
in the ProfileCLassType class, have a date time. update it as the player plays at acceptable intervals and then when they close it, you can load the file using a similar code later. Check ‘data persistence unity’.
After you’ve crafted your class to save the data for the time when they were last using it, and loaded that file, you’ll just want to simply subtract time and then evaluate the amount to see how much energies you wanna give em.