- Home /
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_);
}
}
OnApplicationPause(bool pauseStatus) is called when the application is paused with pauseStatus = true, and also called when the application is unpaused with pauseStatus = false.
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.
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.
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?
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?
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 :)
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/
Tried with OnApplicationFocus - same issue.
Also - I can't seem to find the 'RunInBackground' check in the Player Settings.
Actually here's the setting in the player settings:
https://docs.unity3d.com/$$anonymous$$anual/class-PlayerSettingsStandalone.html
It's under "Resolution and Presentation"
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.
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).