Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Eugenius · Nov 06, 2017 at 08:23 PM · c#unity 5onapplicationpause

OnApplicationPause weirdly triggered in Editor

Hi everyone,

I'm curious if anyone has faced this previously - I'm working on something that should rely on an application pause function, however, while testing the OnApplicationPause function using breakpoints, the following code would return different values for the bool every update cycle (first time false, second time true and it kept on going). Additionally, I'm curious why the function would be called multiple times, since the documentation mentions that it should be called when the Application is paused.

The code I'm using to test is the following:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using SimpleJSON;
 using System;
 
 public class TimeManagement : MonoBehaviour
 {
     string time_str;
     
     // Use this for initialization
     IEnumerator GetTime()
     {
         // Get Time
         string currentTime = "http://www.optimoose.eu/_get/util/time";
         WWW www = new WWW(currentTime);
         yield return www;
 
         if (www.error == null)
         {
             time_str = www.text;
         }
     }

     void OnApplicationPause(bool pauseStatus)
     {
         if (pauseStatus)
         {
            // StartCoroutine(GetTime());
             PlayerPrefs.SetString("startTime", time_str);
 
             CompareTime();
         }
         else
         {
 
             //StartCoroutine(GetTime());
             PlayerPrefs.SetString("pausedTime", time_str);
         }
     }
 
     public void CompareTime()
     {
   
         double a_ = Convert.ToDouble(PlayerPrefs.GetString("pausedTime"));
 
         double b_ = Convert.ToDouble(PlayerPrefs.GetString("startTime"));
 
         DateTime dt_a = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
         dt_a = dt_a.AddSeconds(a_);
 
         DateTime dt_b = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
         dt_b = dt_a.AddSeconds(b_);
 
     }
 }
Comment
Add comment · Show 2
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 pako · Nov 07, 2017 at 09:55 AM 0
Share

OnApplicationPause(bool pauseStatus) is called when the application is paused with pauseStatus = true, and also called when the application is unpaused with pauseStatus = false.

avatar image Eugenius pako · Nov 07, 2017 at 09:59 AM 0
Share

Yes - I read the documentation - however, if I add a breakpoint on the function, when I pause the editor, I get constant updates on the functions (not one) and they are not the same but more like:

pauseStatus = true; pauseStatus = false; pauseStatus = true; pauseStatus = false;

In this situation, it's impossible to run any proper code on the function, at least for me.

3 Replies

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

Answer by pako · Nov 07, 2017 at 10:21 AM

This happens to me too, without even pausing the editor. It's because of the break points. When the breakpoint is hit the application pauses as VS is brought in the foreground with the breakpoint highlighted. Then when you "Continue" execution the focus goes back to the editor and the application unpauses. Now, when you factor-in that you manually pause/unpause the editor, the effect is multiplied.

The only way that I have found a workaround, is to not use any breakpoints when I use OnApplicationPause, just because the breakpoints mess-up the functionality. So, I use Debug.Log statements, as necessary, to examine the values I need.

Comment
Add comment · Show 4 · 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 Eugenius · Nov 07, 2017 at 10:26 AM 0
Share

Hmm - indeed, it seems you are right (breakpoints messing functionality).

However, it also seems that pausing/unpausing the editor doesn't trigger OnApplicationPause while simply clicking outside of Unity does - so how should this be tested in this case?

avatar image pako Eugenius · Nov 07, 2017 at 12:43 PM 0
Share

So, if you just click outside of Unity and then back in, to pause/unpause, isn't that good enough for testing? If not, what is the specific functionality you want to test, with OnApplicationPause being triggered by Editor pause/unpause?

I mean, if it's just OnApplicationPause that you want to test, it doesn't matter how it gets triggered. So, clicking outside of Unity and then back in, should do the trick. Or, what I'm I missing here?

avatar image Eugenius pako · Nov 07, 2017 at 12:44 PM 0
Share

True - although I was expecting the actual 'Pause' button to trigger the pause.

Thanks a lot for the help - if you can upgrade the comment to an answer I can mark it as solved :)

Show more comments
avatar image
1

Answer by TanselAltinel · Nov 06, 2017 at 09:13 PM

Check your player settings to see if RunInBackground is checked. Aside from that, whenever your focus is out of Game View, it will be triggered in editor. So it is kinda normal for it to act like that.

Instead that, using OnApplicationFocus might give more solid responses.


Check these topics too:

https://answers.unity.com/questions/496290/can-somebody-explain-the-onapplicationpausefocus-s.html

https://forum.unity.com/threads/onapplicationpause-not-working-in-the-editor.431057/

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 Eugenius · Nov 07, 2017 at 09:42 AM 0
Share

Tried with OnApplicationFocus - same issue.

Also - I can't seem to find the 'RunInBackground' check in the Player Settings.

avatar image TanselAltinel Eugenius · Nov 07, 2017 at 10:51 AM 0
Share

Try with this: https://docs.unity3d.com/ScriptReference/Application-runInBackground.html

avatar image Bunny83 Eugenius · Nov 07, 2017 at 01:13 PM 1
Share

Actually here's the setting in the player settings:

https://docs.unity3d.com/$$anonymous$$anual/class-PlayerSettingsStandalone.html

It's under "Resolution and Presentation"

avatar image
0

Answer by JasonBennett · Nov 06, 2017 at 09:10 PM

Would you mind sharing the whole code for the Class that is calling OnApplicationPause( bool )? There may be something happening outside the method that is causing the fluctuation.

Comment
Add comment · Show 1 · 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 Eugenius · Nov 07, 2017 at 09:40 AM 0
Share

Updated the code so the entire is there.

I thought this as well and checked if the co-routine could be causing it (it doesn't seem like it).

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

444 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Gradle is failed to fetch dependencies , admob ,in UNITY console , Gradle failed to fetched dependencies 3 Answers

Pinch Zoom and Pan while not calling other functions 1 Answer

Need to a Instantiate a prefab in canvas which is in another scene 1 Answer


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