当前位置:网站首页>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);}}
边栏推荐
- Image stitching (registration) case based on OpenCV
- The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Partial WP (10)
- 机器学习:知道通过低方差过滤实现降维过程
- 基于OpenCV实现的图像拼接(配准)案例
- 厦门感芯科技MC3172(1):介绍和环境搭建
- 【MySQL系列】-B+树索引和HASH索引有什么区别
- @WebServlet注解(Servlet注解)
- DAY17: weak password detection and test
- 恐造成下一个“千年虫”的闰秒,遭科技巨头们联合抵制
- handler+message【消息机制】
猜你喜欢
随机推荐
深圳见!云原生加速应用构建专场:来看云原生 FinOps、SRE、高性能计算场景最佳实践
KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!
DAY17: weak password detection and test
Repetition XXL - JOB scheduling center background arbitrary command execution
Go书籍大全-从初级到高级以及Web开发
How with Mexico Volkswagen VW EDI connection to Mexico
1. Get data - requests.get()
VUX Datetime 组件compute-days-function动态设置日期列表
Azure 开发者新闻快讯丨开发者7月大事记一览
05全局配置文件application.properties详解
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
QT(39)-vs开发qt程序提示无法打开源文件
[SQL] at a certain correlation with a table of data update another table
05 Detailed explanation of the global configuration file application.properties
[C language] Program environment and preprocessing
Classification of decision tree classification
2.5 Quick Sort
Advanced [C] array to participate in the function pointer
Introduction to Thymeleaf
数据库概论 - MySQL的简单介绍