Answer the question
In order to leave comments, you need to log in
Unity time away calculation?
private void OnApplicationQuit()
{
Save();
}
public void Save()
{
PlayerPrefs.SetString("Last", DateTime.Now.ToString());
}
TimeSpan ts = DateTime.Now - DateTime.Parse(PlayerPrefs.GetString("Last"));
float time = ts.Hours * 3600 + ts.Minutes * 60 + ts.Seconds;
TheTime -= time;
Answer the question
In order to leave comments, you need to log in
float time looked exactly? or immediately TheTime??
(although the local format for saving time probably does not take into account seconds. It saves only minutes in a string) it’s almost certainly a matter of the format))
when they checked with the server, the time was stored in milliseconds.
and also in UTC, so that there are no problems with the local time format, etc. just the same number for all)
//save
long currenttime = System.DateTime.UtcNow.Ticks;
PlayerPrefs.SetString("key", currenttime.ToString());
//load
long savedTime = long.Parse(PlayerPrefs.GetString("key", currenttime.ToString()));
System.TimeSpan timeSpan = System.DateTime.UtcNow - new System.DateTime(savedTime);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question