简体   繁体   English

当我在unity3d中关闭应用程序时保持计时器运行

[英]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 . 我目前正在开发一个统一游戏,希望每5小时重置一次计数器,例如,即使我关闭应用程序,我也面临着保持此计时器处于活动状态的问题:如果这是我第一次打开应用程序,则计时器为0 ,直到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 如果我在5 hrs finish前关闭了应用程序,而我稍后又回来了,那么我应该看到自上次关闭应用程序以来经过的时间,并compare it again to see if it reached 5 hrs or no

I am currently developing this app for unity android 我目前正在为统一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();
  }

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 您可以在此处找到有关在场景之间以及应用程序执行之间保持数据的教程: 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. 您特别需要的是在游戏关闭时保存current_min和sec值,并在用户下次启动游戏时将其重新加载。

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. 您还可以考虑不计算秒数,而是存储上一个“事件”的日期时间值,然后简单地检查当前日期时间是否等于或大于前一个时间+ 5小时。

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. 因此,请勿将这种方法用于敏感的事物,例如试玩限制或多人游戏中的acton。 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. 您仍然缺少在调用调用计时器之前在启动时加载ti的部分。

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. 简单地破解这一功能是微不足道的,但是您应该对游戏进行设置,以使其能够优雅地处理应用执行之间的恢复游戏玩法,因此您需要进行更多研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM