I have to refill the energy after some minutes. If the user close the game and come back then the remaining time should be calculated and the 1 energy should be filled. Can anyone help ???
I suppose you could use playerprefs to keep the time saved.
Here is an example, its pseudo untested code but you might get the idea how to tackle this.
// Check if we already have a date set
var unParsedTime = PlayerPrefs.GetString("systemTime");
if (string.IsNullOrEmpty(unParsedTime))
{
unParsedTime = DateTime.Now.ToString();
PlayerPrefs.SetString("systemTime", unParsedTime);
PlayerPrefs.Save();
}
var lastTime = DateTime.Parse(unParsedTime);
TimeSpan span = DateTime.Now.Subtract(lastTime);
if (span.TotalMinutes >= 2) // After 2 minutes
{
// Do your business
// Update the system time to now
PlayerPrefs.SetString("systemTime", DateTime.Now.ToString());
PlayerPrefs.Save();
}