Version: 2018.4
LanguageEnglish
  • C#

MonoBehaviour.OnApplicationPause(bool)

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Parameters

pauseStatus True if the application is paused, else False.

Description

Sent to all GameObjects when the application pauses.

OnApplicationPause is set to true or false. Normally, false is the value returned by the OnApplicationPause message. This means the game is running normally in the editor. If an editor window such as the Inspector is chosen the game is paused and OnApplicationPause returns true. When the game window is selected and active OnApplicationPause again returns false. True means that the game is not active.

See Resolution and Presentation in the Player Settings... to turn off Run in Background and Visible in Background.

OnApplicationPause can be used in an independent game running separately from the editor. The running game needs to be windowed and smaller than the full screen. If the game is hidden (fully or partly) by another application then OnApplicationPause will return true. When the game is changed back to current it will no longer be paused and OnApplicationPause will return to false.

OnApplicationPause can be a co-routine; to do this use the yield statement in the function. Implemented this way, it is evaluated twice during the initial frame: first as an early notification, and secondly during the normal co-routine update step.

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 instead.

Note: MonoBehaviour.OnApplicationPause receives true or false. There is no way to call this message. Also the keyboard/mouse/etc has no way to control MonoBehaviour.OnApplicationPause. Pause means the game is running normally or has been suspended.

Note: MonoBehaviour.OnApplicationPause is called as a GameObject starts. The call is made after Awake. Each GameObject will cause this call to be made.

using UnityEngine;

public class AppPaused : MonoBehaviour { bool isPaused = false;

void OnGUI() { if (isPaused) GUI.Label(new Rect(100, 100, 50, 30), "Game paused"); }

void OnApplicationFocus(bool hasFocus) { isPaused = !hasFocus; }

void OnApplicationPause(bool pauseStatus) { isPaused = pauseStatus; } }