- Home /
saving total playtime of app?
hello!
i am creating a unity game for android and i have a question.
i would like the total playing time to be stored somehow, so that players can see how long they already play the game.
the best thing would be if this value is somehow stored together with the app in the appstore (i.e. if someone loses his phone and installs the game again, his time played is not lost)
but if this is not possible or too difficult to code the second option would be to store the time value on the phone as "local savegame" (so if the game is uninstalled the total playing time is gone)
in both cases i have absolutely no idea how to approach the saving problem. by now i have nearly finished the project with gameplay etc, but i never used savegames before.
anyone can help me or tell me what to do best step by step? thanks! m.
Answer by TonyLi · Mar 13, 2014 at 02:19 PM
Try saving it locally first. Use PlayerPrefs:
void SavePlaytime(float playtime) {
PlayerPrefs.SetFloat("Playtime", playtime);
}
And use PlayerPrefs.GetFloat("Playtime") to retrieve the value stored in the key "Playtime". Read this answer for information about permission to write to SD.
You can use Time.realtimeSinceStartup, or Time.time if you want to ignore time spent paused. (Time.time doesn't progress if Time.timeScale==0.) Note that if you go into slow motion (e.g., Time.timeScale = 0.5), this will affect Time.time too.