[Unity计时器倒计时,当应用杀死Android时不起作用

与天地兮比寿,与日月兮齐光。这篇文章主要讲述[Unity计时器倒计时,当应用杀死Android时不起作用相关的知识,希望能为你提供帮助。
我对unity和c#还是陌生的,我有一个代码可以在10m后增加能量,我已经使用PlayerPrefs这样做,所以它可以统一工作,但不能在移动设备上工作,请帮忙。我无法理解到底是什么导致了电话问题。当应用程序未终止但在移动设备上最小化时,计时器也会暂停。我希望计时器倒数继续使游戏最小化,并且如果被杀死也可以。

using System; using UnityEngine; using UnityEngine.UI; public class EnergyAdder : MonoBehaviour { // Start is called before the first frame update public float waitTime = 600; Timer timer; public GameRoot gameRoot; public int count; DateTime currentDate; DateTime oldDate; void Start() { //timer = new Timer(waitTime); float remainingTime = PlayerPrefs.GetFloat("TimeOnExit"); timer = new Timer(remainingTime); if (PlayerPrefs.HasKey("sysString")) { currentDate = DateTime.Now; long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString")); oldDate = DateTime.FromBinary(temp); TimeSpan difference = currentDate.Subtract(oldDate); count = (int)(difference.TotalSeconds / waitTime); Debug.Log("time remaining " + count); gameRoot.AddEnergy(count); timer = new Timer(remainingTime - (float)difference.TotalSeconds); //PlayerPrefs.DeleteKey("TimeOnExit"); //PlayerPrefs.DeleteKey("sysString"); } }// Update is called once per frame void Update() { if (Gameroot.isFull) { GetComponent< Text> ().text = "Full"; count = 0; timer.refresh(); } else { //Debug.Log("deltatime ************************"+ secondstime); timer.countDown(); if (timer.isFinished()) { timer = new Timer(waitTime); timer.refresh(); gameRoot.AddEnergy(1); } UpdateClock(); } }void UpdateClock() { int seconds = ((int)timer.timeLeft) % 60; int minutes = (int)(timer.timeLeft / 60); GetComponent< Text> ().text = minutes.ToString() + ":" + seconds.ToString("00"); }void OnApplicationQuit() { PlayerPrefs.SetFloat("TimeOnExit", timer.timeLeft); var today = DateTime.Now; PlayerPrefs.SetString("sysString", today.ToBinary().ToString()); PlayerPrefs.Save(); } }

【[Unity计时器倒计时,当应用杀死Android时不起作用】请帮助上面代码中的错误或缺失。
答案我认为您可以使用OnApplicationPauseOnApplicationPause
[对于OnApplicationFocusOnApplicationFocus,也要做与OnApplicationQuit中相同的操作>
并且与OnApplicationPause(true)OnApplicationFocus(false)Start之类的操作相同
OnApplicationPause(false)


    推荐阅读