Using a timer for regenerating player energy
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.
Answer by andrew_2992 · Jul 01, 2016 at 08:04 PM
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.
Just wanted to mention too, if you do want to do this more safely... and about the same effort as well, you can contact a server. this can be a free php server you have or a cheap paid one, either or. also you can use cgi asp.net whatever you want as long as you use an http connection. For that check out the wwwform class in unity. Its basically a web browser for unity except the web page will come back as data ins$$anonymous$$d of being formatted into a nice page like our browsers do.
Your answer
Follow this Question
Related Questions
Merging two projects into one 0 Answers
Mobile joystick OnPointerUp causes error 1 Answer
Mobile Build laggy? 0 Answers
Terrain creation for android 1 Answer
Cardboard VR: "LookWalk" works in Unity, but not when I build & run to a mobile phone 1 Answer