- Home /
Unity iOS - Increase Value Based On Time Inactive
Hi there! I'm developing an iOS game in Unity that includes a recharging currency. The currency recharges by 1, every minute that the player isn't playing. How would I go about doing this? Is there a value or function in Unity to get unix (epoch) time? Really if someone can answer that, I can figure out the rest.
Will post with update once I get it working.
When you mean 'isnt playing', do you mean by when the application is closed down and not running on their phone?
Both, so when the application is exited, or running in the background. Also, when the device is off, and so on. It'll be calculated by the difference in time from when they last played and when they start again.
Answer by Meltdown · Sep 18, 2011 at 06:42 PM
For the first scenario - when the game goes into the background, you can get a timestamp from the OnApplicationFocus method (this event fires when the game loses, or receives focus). So as soon as the application loses focus, store the timestamp, then once the game receives focus, subtract the two dates from each other, and use that to calcuate the currency.
You can use the same technique for OnApplicationQuit, just make sure the timestamp is stored in some sort of persistent data store on the mobile device.
Unfortunately, the OnApplicationQuit and OnApplicationFocus don't work for me in this case. The device doesn't detect the focus, or at least it doesn't detect it the same way the Unity Editor does.