Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计数器/计时器可以在后台运行吗?_C#_Unity3d_Background Process - Fatal编程技术网

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

C# 计数器/计时器可以在后台运行吗?,c#,unity3d,background-process,C#,Unity3d,Background Process,我可以在后台运行计时器吗。当我最小化游戏时,我的计时器应该工作。我能做什么 我正在尝试Application.runInBackground=true但它不能工作 public class Counter : MonoBehaviour { public Text counterText; private int counterValue; // Use this for initialization void Star

我可以在后台运行计时器吗。当我最小化游戏时,我的计时器应该工作。我能做什么

我正在尝试
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");
         }
 }

你可以这样做

使用脚本检查应用程序是否最小化,检查应用程序的状态是否可以使用,
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

我已经找到了我问题的答案。 特别感谢迪纳尔先生。 在迪纳尔先生的帮助下,我可以通过更新一些资料得到我的答案,这对我很有帮助

注意:此代码适用于ANDROID和IOS(IOS必须要求UNITY 4.6.1或更高版本)


您可以使用DateTime查找系统时间,然后使用TimeSpan查找从最小化到重新打开所用的时间。Savlon>此时间由我定义。我不想使用系统时间。你们还有别的办法吗?嗨,Dinal24>不是这样的,我每隔一秒钟增加一次计数器变量的值。假设我把我的游戏放在最小化,那么计数器也会在后台进程中增加。当我最大化该游戏时,更新的计数器值将显示在我的textmesh中。@SudhirKotila我明白你的意思!这给出了相同的结果,它并不是每秒钟增加一次,而是反映了计数器当时应该收到的值。这也更方便。@Dinal24如何调用这两种方法?如何使游戏最小化或最大化?@SudhirKotila您可以使用windows上的应用程序聚焦
,可能需要为其他平台找到一些东西。>您的反应非常好。是的,你的逻辑是成功的,谢谢你。
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;
                }

        }


}