Version: 2020.3
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

Switch to Manual

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; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961