Sora
Sora

Reputation: 2551

Keep a timer running when i close the application in unity3d

I'm currently developing a unity game wish have a counter that reset every 5 hrs , i am facing a problem in keeping this timer active even when i close the application for example : if it was the first time i open the app the timer is 0 and keep getting up till it reach 5 hrs. if i close the app before the 5 hrs finish and i come back later i should see the amount of time passed since the last close of the app and compare it again to see if it reached 5 hrs or no

I am currently developing this app for unity android

What I did so far :

  public static int Sec=PlayerPrefs.GetInt("Sec",0);
  public static int Current_Min=PlayerPrefs.GetInt("CurrentMin",0);
  public static int Max_min = PlayerPrefs.GetInt("MaxMin",5*60);

  void Start()
  {
       InvokeRepeating("GetDate",0,1f);
  }

  void GetDate()
  {
        Sec++;
        if (Sec == 60){
            Sec = 0;
            Current_Min++;
       }
       if(Current_Min==Max_min)
       {
          // do something ....
       }
  }
  void OnApplicationQuit()
  {
     PlayerPrefs.SetInt("Sec",Sec);
     PlayerPrefs.SetInt("CurrentMin",Current_Min);
     PlayerPrefs.SetInt("MaxMin",Max_min);
     PlayerPrefs.Save();
  }

Upvotes: 0

Views: 5571

Answers (1)

irreal
irreal

Reputation: 2296

What you are asking for is a way to persist a piece of information between play sessions of your user. Data persistance is a broader concept that is used throughout game development. Ie, save games, high scores, user preference settings, etc.

You can find a tutorial on persiting data between scenes as well as between app execututions here: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

What you specifically need is to save current_min and sec values on game close and load them back up the next time the user starts your game.

You can also consider not counting up seconds, but rather storing a date time value of the last "event" and then simply checking if current date time is equal or higher than the previous time + 5 hours.

Note that neither of these approaches is inherently "safe" from hinderence by the user. Ie, deleting your game's user data or changing the system clock, etc.

So do not use this approach for sensitive things such as trial limitations or actons in multiplayer games. For that, you need outside verification / authorization that doesn't rely on information stored on the user's device

EDIT

You have edited your original question and inserted the part that saves data to player preferences on close. You are still missing the part which would load ti back up on start, before calling the invoke timer.

I strongly suggest you go through the entire tutorial I originally linked, as it will show you the correct way to handle these sort of things.

You did not explain what you are using the "timer" for, but I suspect it should be a part of a larger game save / load process that you need to implement for your users. Simply hacking in this one functionality is trivial, but you should setup your game such that you gracefully handle resuming gameplay between app executuions, and for that you need to research more.

Upvotes: 4

Related Questions