Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by sudhir_kotila · Feb 18, 2015 at 06:30 AM · javascriptwebplayerunity4.6

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");
         }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HarshadK · Feb 18, 2015 at 06:35 AM 0
Share

What platform you are developing this for?

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by sudhir_kotila · Feb 19, 2015 at 06:54 AM

I have found answer of my question.

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;
                 }
 
         }
 
 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jeromeWork · Mar 14, 2017 at 09:44 AM 0
Share

Why the focusCounter on line55? [`if (focusCounter >= 2) {`]

Why would you want the app to recalculate its counter only after the second time it is refocused?

avatar image jeromeWork jeromeWork · Mar 14, 2017 at 09:47 AM 0
Share

Ah! I think I worked it out... I'm guessing that the first time the app is focused is on initial load.

avatar image rm-square · Mar 16, 2020 at 08:17 PM 1
Share

Take care with OnApplicationPause and OnApplicationFocus. You should normally work with OnApplicationPause status. The Documentations https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnApplicationPause.html says: "On Android, when the on-screen keyboard is enabled, it causes a OnApplicationFocus(false) event. Additionally, if you press "Home" at the moment the keyboard is enabled, the OnApplicationFocus() event is not called, but OnApplicationPause() is called ins$$anonymous$$d."

In the documentation link, there is an example to workaround the dangerous part with Focus and Pause.

avatar image
3

Answer by zharik86 · Feb 18, 2015 at 07:21 AM

Coroutines run in the same thread as your function Update(). If you use target platform as Android, you could build a plugin. Or use OnApplicationPause and calculate the time passed in the application and do whatever it is you were wanting to do in the time frame between.

  void OnApplicationPause(bool paused) {
   if(paused) {
    //Save the time
   } else {
    //Recalculate the time
   }
  }

I hope that it will help you.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by elmar1028 · Feb 18, 2015 at 06:53 AM

If you want your game to run in background constantly I would suggest you to go to Player Settings and make sure "Run in Background" is checked.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sudhir_kotila · Feb 18, 2015 at 06:58 AM 0
Share

Elmar1028 > I am try that through coding but not works. have you any other way for do this?

avatar image seanmoebeal · Nov 02, 2015 at 06:37 PM 0
Share

Note: this works with Standalone, Blackberry, Windows App, and WebGL settings, but not Android, iPhone, or Windows 8 Phone.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[closed]how to match boxcollider2d size with image after preserv aspect? 4 Answers

How to get Profiler window into my project? 1 Answer

Ignore(absorb) Raycast on Canvas in mobile 1 Answer

xcode linker error 0 Answers

Screen record in unity3d 7 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges