OnApplicationPause/OnApplicationFocus has been all over the place for me. In the Editor, it seems to be different on PC and MAC. On MAC it seems to get called on press Play. On PC it doesn’t. On iOS it seems like one of them doesn’t work. Can somebody tell me the different scenarios that they work?
And can somebody explain what they mean in each scenario? Editor? Standalone? iOS? Android? (like does OnApplicationFocus fire when you minimize or when you just click on another window in the Editor)
I know about the documentation, I read the documentation. But the behavior doesn’t match it. It’s ridiculous. If somebody has figured it out I’d greatly appreciate it.
Since this UnityAnswer is one of the first (if not the first) to be returned on a search for OnApplicationFocus/Pause & iOS, an important update in Unity 4.6.1 has changed the behavior for iOS.
As of 4.6.1, both OnApplicationFocus and OnApplicationPause will be called in iOS.
The order is :
App initially starts:
- OnApplicationFocus(true) is called
App is soft closed:
App is brought forward after soft closing:
Hope that helps
Btw, tested on Android API 5+ and iOS 12+, using Unity 2018.2, here is what I’m using :
#if UNITY_ANDROID
void OnApplicationFocus ( bool focus )
{
if ( focus ) ResumeApplication ();
else LeaveApplication ();
}
#endif
#if UNITY_EDITOR || UNITY_IOS
void OnApplicationPause ( bool pause )
{
if ( pause ) LeaveApplication ();
else ResumeApplication ();
}
#endif
OnApplicationPause is what gets called when you soft close on an iOS device.
OnApplicationFocus does the same thing but for windows. OnApplicationFocus can be tested inside of the Unity editor if you click out of the game screen for instance it will be called.
I’ve included a very simple test that I have used for a current game which is built for multiple devices.
void Update()
{
if (Input.GetKey("p"))
{
OnApplicationPause(true);
//OnApplicationFocus(true);
}
}
/*void OnApplicationFocus(bool pauseState)
{
paused = pauseState;
if (paused == true)
{
loginSoftClose.SetActive(true);
}
}*/
void OnApplicationPause(bool pauseState)
{
paused = pauseState;
if (paused == true)
{
loginSoftClose.SetActive(true);
}
}
The best answer is in this video
On latest versions of Unity/iOS, we’re noticing that OnApplicationFocus ISNT called when the app launches.