计数器/计时器可以在后台运行吗?

时间:2015-02-18 06:33:37

标签: c# unity3d background-process

我可以在后台运行计时器吗?当我最小化游戏时,我的计时器应该继续工作吗?

我试过Application.runInBackground=true;,但它不起作用。

public class Counter : MonoBehaviour
 {
         public Text counterText;
         private int counterValue;

         // Use this for initialization
         void Start ()
         {
                 Application.runInBackground=true;
                 StartCoroutine ("StartCounter");
         }

         IEnumerator StartCounter ()
         {
                 yield return new WaitForSeconds (1f);
                 counterText.text = "Counter : " + counterValue.ToString ();
                 counterValue++;
                 StartCoroutine ("StartCounter");
         }
 }

2 个答案:

答案 0 :(得分:6)

我找到了问题的答案。 特别感谢Mr.Dinal24。 在Mr.Dinal24的帮助下,我可以通过更新一些内容得到我的答案,这对我非常有帮助。

注意:此代码适用于安卓和IOS(适用于IOS必须使用4.6.1或以上)

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;

public class Counter : MonoBehaviour
{

        public Text counterText, pauseText, resumeText, msgText;

        private int counterValue, focusCounter, pauseCounter;
        private DateTime lastMinimize;
        private double minimizedSeconds;

        void OnApplicationPause (bool isGamePause)
        {
                if (isGamePause) {
                        pauseCounter++;
                        pauseText.text = "Paused : " + pauseCounter;
                        GoToMinimize ();
                } 
        }

        void OnApplicationFocus (bool isGameFocus)
        {
                if (isGameFocus) {
                        focusCounter++;
                        resumeText.text = "Focused : " + focusCounter;
                        GoToMaximize ();
                } 
        }

        // Use this for initialization
        void Start ()
        {
                StartCoroutine ("StartCounter");
                Application.runInBackground = true;
        }

        IEnumerator StartCounter ()
        {
                yield return new WaitForSeconds (1f);
                counterText.text = "Counter : " + counterValue.ToString ();
                counterValue++;
                StartCoroutine ("StartCounter");
        }

        public void GoToMinimize ()
        {
                lastMinimize = DateTime.Now;
        }

        public void GoToMaximize ()
        {
                if (focusCounter >= 2) {
                        minimizedSeconds = (DateTime.Now - lastMinimize).TotalSeconds;
                        msgText.text = "Total Minimized Seconds : " + minimizedSeconds.ToString ();
                        counterValue += (Int32)minimizedSeconds;
                }

        }


}

答案 1 :(得分:2)

你可以这样做,

有一个脚本来检查应用程序是否已最小化,要检查应用程序的状态是否可以使用OnApplicationFocus

DateTime lastMinimize;
int timer; // what ever the type you want

// change the value when you game is sent to background
// make sure this is changed before actual minimize happen
public void aboutToMinimize(){
    lastMinimize = DateTime.Now;
}

public void gotMaximized() {
    timer = (DateTime.now - lastMinimize).getMillis();
}
//now use the timer value to reset the timer