Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can counter/timer run in background?

Can i run timer in background. When i minimize game then my timer should be works continue can i do ?

I was try Application.runInBackground=true; but it can't works.

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");
         }
 }
like image 924
Sudhir Kotila Avatar asked Feb 18 '15 06:02

Sudhir Kotila


2 Answers

I have found answer of my question. Special Thanks to Mr.Dinal24. With the help of Mr.Dinal24 i can get my answer with update some stuff and it's very much helpful to me.

NOTE : THIS CODE WORKS FOR ANDROID AND IOS BOTH (FOR IOS MUST REQUIRED UNITY 4.6.1 OR ABOVE)

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;
                }

        }


}
like image 60
Sudhir Kotila Avatar answered Nov 17 '22 03:11

Sudhir Kotila


You can do it like this,

Have a script to check if the application is minimized, To check if the application's state you can use, 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
like image 2
Dinal24 Avatar answered Nov 17 '22 03:11

Dinal24