pauseStatus | 如果应用程序暂停,则为 true,否则为 false。 |
当应用程序暂停时,发送给所有 GameObject。
OnApplicationPause 设置为 true 或 false。
通常情况下,OnApplicationPause 消息返回 false 值。
这意味着游戏正在 Editor 中正常运行。
如果选择了某个 Editor 窗口(例如 Inspector),
游戏将暂停并且 OnApplicationPause 返回 true。
当选中并激活游戏窗口时,OnApplicationPause 将再次返回 false。
true 表示游戏未激活。
请参阅 Player Settings...
中的 Resolution and Presentation
以
关闭 Run in Background
和 Visible in Background
。
OnApplicationPause 可以在独立于 Editor 运行的游戏中使用。
该游戏需要以小于全屏的窗口模式运行。
如果游戏被其他应用程序(全部或部分)遮挡,
OnApplicationPause 将返回 true。
当游戏重新变为当前窗口时,它将取消暂停状态,
并且 OnApplicationPause 返回 false。
OnApplicationPause 可以作为协同程序使用 -
在函数中使用 yield 语句即可。以这种方式实现时,将在初始帧期间对其计算两次:
第一次是早期通知,第二次在正常的协同程序更新步骤期间进行。
在 Android 上,当启用屏幕键盘时,它会导致 OnApplicationFocus(false) 事件。
此外,如果在启用键盘时按“Home”,则不会调用 OnApplicationFocus() 事件,
而改为调用 OnApplicationPause()。
**注意:**MonoBehaviour.OnApplicationPause 接收 true 或 false。
您无法调用该消息。此外,键盘/鼠标等也无法控制 MonoBehaviour.OnApplicationPause。
暂停意味着游戏正常运行或已被暂停。
**注意:**MonoBehaviour.OnApplicationPause 在 GameObject 启动时调用。该调用在 Awake
之后进行。每个 GameObject 都会导致该调用。
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; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.