当前位置:网站首页>Unity3D Application simulation enters the front and background and pauses
Unity3D Application simulation enters the front and background and pauses
2022-07-30 04:34:00 【WenHuiJun_】
Foreword
At some point, the unity program runs in the background, and we need it to pause or close some functions, such as the currently playing animation, video, sound, etc., and continue to run the next time we return to the unity program.
Here we need to use two unity callback functions OnApplicationForcus and OnApplicationPause,Use the SendMessage method to send instructions, simulate and end the background operation, and automatically call OnApplicationForcus andOnApplicationPause method.
To close the background running of Unity3D for Windows, you can cancel Run In Background in PlayerSetting.

Reproduced below: https://blog.csdn.net/aa4790139/article/details/48087877
Normal progress:
OnApplicationFocus, isFocus=True
Normal Return:
OnApplicationQuit
Home out:OnApplicationPause, isPause=True
OnApplicationFocus, isFocus=False
Home entry:
OnApplicationPause, isPause=False
OnApplicationFocus, _isFocus=TrueKill Process:
Double-click Home for the current application, then Kill:
OnApplicationQuit (IOS has a callback, android has no callback)
Jump out of the current application, then Kill:
OnApplicationQuit (no callback for IOS and Android)
Reproduced below: https://www.jianshu.com/p/cfa0263ea1b9
The easiest way to pause the game
Time.timeScale = 0;When you want to continue playing
Time.timeScale = 1;Note
When TimeScale is set to 0,
- Update method will still be called
- FixedUpdate method will not be called
- Time.time will not continue to increase
- Time.deltaTime will be affected
If you don't want to be affected by TimeScale
Time.unscaledDeltaTime replaces Time.deltaTime
Time.fixedUnscaledDeltaTime replaces Time.fixedDeltaTime
Time.unscaledTime replaces Time.timeCoroutines
Replace Time.deltaTime with Time.unscaledDeltaTime
Replace WaitForSeconds with WaitForSecondsRealtimeAnimation Controller
CullingMode is set to UnscaledTime
How to pause audio playback
AudioListener.pause = true;When the pause is over
AudioListener.pause = false;What if I want to continue playing some sound effects when I paused?
AudioSource.ignoreListenerPause = true;
Simulation code snippet:
void Update(){if (Input.GetKeyDown(KeyCode.A))SendMessageTest(true);else if (Input.GetKeyDown(KeyCode.S))SendMessageTest(false);}private void OnApplicationForcus(bool isForcus){if (isForcus){Debug.Log("isForcus:true");}else{Debug.Log("isForcus:false");}}private void OnApplicationPause(bool isPause){if (isPause){Debug.Log("isPause:true");Time.timeScale = 0;AudioListener.pause = true;}else{Debug.Log("isPause:false");Time.timeScale = 1;AudioListener.pause = false;}}void SendPause(bool isPause){transform.SendMessage("OnApplicationPause", isPause, SendMessageOptions.DontRequireReceiver);}void SendForcus(bool isForcus){transform.SendMessage("OnApplicationForcus", isForcus, SendMessageOptions.DontRequireReceiver);}public void SendMessageTest(bool pause){if (pause){SendPause(true);SendForcus(false);}else{SendPause(false);SendForcus(true);}}边栏推荐
- [Linear table] - Detailed explanation of three practice questions of LeetCode
- Thinkphp 5.0.24 Variable Override Vulnerability Causes RCE Analysis
- The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Partial WP (10)
- 2.5 Quick Sort
- 使用EFR32作为Zigbee/Thread的sniffer的用法
- [MRCTF2020]Hello_ misc
- What is the data directory?Why do you need it?
- Android Studio implements login registration - source code (connecting to MySql database)
- 机器学习:知道通过低方差过滤实现降维过程
- @ WebServlet annotations (Servlet annotations)
猜你喜欢

My first experience of Go+ language——Blessing message system, so that she can also feel your blessings

使用EFR32作为Zigbee/Thread的sniffer的用法

A must see for software testers!Database knowledge MySQL query statement Daquan

解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b

2.6 Radix sort (bucket sort)

How does MySql find out the latest data row that meets the conditions?

Azure 开发者新闻快讯丨开发者7月大事记一览

MySQL operation statement Daquan (detailed)
![[Awards every week] The](/img/78/4b510b190475d603490614d2c8199f.png)
[Awards every week] The "Edge Containers" track of the Cloud Native Programming Challenge invites you to fight!

Thymeleaf简介
随机推荐
2.6归并排序
2021 Shandong Province Network Construction and Application Test Questions
Charles 替换 接口响应信息
[Linear table] - Detailed explanation of three practice questions of LeetCode
Usage of EFR32 as sniffer for Zigbee/Thread
解决go环境编译不了exe
BGP的简单实验
代码开源设计实现思路
小程序 wx.miniProgram.navigateTo 跳转地址不能是tabbar地址
@WebServlet注解(Servlet注解)
Go 学习笔记(84)— Go 项目目录结构
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
验证addShutdownHook钩子生效
GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面
KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!
Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference
[MRCTF2020]Hello_misc
golang八股文整理(持续搬运)
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
C. Travelling Salesman and Special Numbers (二进制 + 组合数)