Hi guys, i am devloping a mobile game right now. On this game the characters are building a monument, and my code counts how many time the player was outside the game and when the player comeback, the building it’s bigger based on the time that player was on another app. However, when the player closes the app outside of the game, unity don’t save the last date time. So the datetime is only stored before the game is closed. All others types of data are stored on Json file when the player reloads the game(like upgrades, the level of monument, etc).
I assume you’re using JsonUtility? It can only serialize what Unity can already serialise.
If you want to use the same serialization method, you will need to store the data in a form Unity can serialise. In this case, there’s already a existing methods to create from and convert a DateTime as a long:
So I’d just store it as a long, honestly.
Yes, i am using json utility, but still not working.
I have a class of type save data that stores almost everything in the game, but i don’t get why this don’t work, even with parse (string to datetime), and now with long.
EDIT: LOOKS LIKE IT WORKED!!! I just forgot to make the conversion on the method onapplicationquit, because now i tested on PC. Thank you so much spiney!!! My game is very close to finish now.
I tried like that:
//first i get the time that player quit the game and stores here, and do the same with the long
dateTimeOfPause = DateTime.Now;
dateTimePauseLong=dateTimeOfPause.ToBinary();
//After that i save the long with my save data class and put that on json file
data.dateOfPauseLong = dateTimePauseLong;
//But when i enter the game again, the datetime it's not correct
dateTimeOfPause= DateTime.FromBinary( dateTimePauseLong );