My playerPrefs arent saving the time for the life countdown correctly. Is there a sample code on this topic? where i can refer to?
Also, if i save my time in playerprefs, then willl it still counting when exited the game?
My playerPrefs arent saving the time for the life countdown correctly. Is there a sample code on this topic? where i can refer to?
Also, if i save my time in playerprefs, then willl it still counting when exited the game?
In order to calculate the time that was lapsed since you last shut down the game, you should save the last time playerprefs in the function OnApplicationPause and calcuate the timelapsed in the Awake Function
void OnApplicationPause(bool isPause){
if (isPause)
{
//save the system time at which the application went in background
PlayerPrefs.SetString("Timer", DateTime.Now.ToString());
}
}
void Awake(){
float timerLapsed = (float)(System.DateTime.Now - Convert.ToDateTime(PlayerPrefs.GetString("Timer"))).TotalSeconds;
}
For those who are still looking out to understand how to create a life counter, you can check out the following link:
http://codesaying.com/life-counter-in-unity/