Hi. I make the virtual pet game.
And I want to make this : If the player don’t play game 24 hours, the game is over.
So what should I write code?
I already made the game over scene, “gameover”.
And here is my code that record the time while the player don’t play the game.
private void saveTime(){
PlayerPrefs.SetString(ResumeKey, TimeUtil.GetUnixTime(System.DateTime.Now).ToString());
}
private long getResumeTime(){
var lastPlayTime = long.Parse(PlayerPrefs.GetString(ResumeKey, "-1"));
return lastPlayTime == -1 ? 0 : TimeUtil.GetUnixTime(System.DateTime.Now) - lastPlayTime;
}
void Start(){
Debug.Log(getResumeTime());
}
void OnApplicationPause(bool pauseStatus) {
if(pauseStatus){
saveTime();
} else {
Debug.Log(getResumeTime());
}
}
void OnApplicationQuit(){
saveTime();
}