当前位置:网站首页>Unity3D Application模拟进入前后台及暂停
Unity3D Application模拟进入前后台及暂停
2022-07-30 04:09:00 【WenHuiJun_】
前言
在某些时候,unity程序进入后台运行,我们需要它暂停或关闭一些功能,例如当前正在播放的动画、视频、声音等等之类的,在下次回到unity程序时再接着运行。
这里我们需要用到两个unity的回调函数OnApplicationForcus和OnApplicationPause,使用SendMessage方法发指令,模拟后台运行跟结束后台运行,自动调用OnApplicationForcus和OnApplicationPause方法。
Unity3D Windows版本若要关闭后台运行,可在PlayerSetting中,取消Run In Background即可。

下面转载于:https://blog.csdn.net/aa4790139/article/details/48087877
正常进:
OnApplicationFocus, isFocus=True
正常退:
OnApplicationQuit
Home出:OnApplicationPause, isPause=True
OnApplicationFocus, isFocus=False
Home进:
OnApplicationPause, isPause=False
OnApplicationFocus, _isFocus=TrueKill进程:
当前应用双击Home,然后Kill:
OnApplicationQuit (IOS 有回调,android 没回调)
跳出当前应用,然后Kill:
OnApplicationQuit (IOS和Android都没回调)
下面转载于:https://www.jianshu.com/p/cfa0263ea1b9
最简单的暂停游戏的方式
Time.timeScale = 0;当想继续游戏时
Time.timeScale = 1;注意
当TimeScale被设置为0时,
- Update方法仍然会被调用
- FixedUpdate方法不会被调用
- Time.time不会继续增加
- Time.deltaTime会被影响
若不想受TimeScale影响
Time.unscaledDeltaTime 替换 Time.deltaTime
Time.fixedUnscaledDeltaTime 替换 Time.fixedDeltaTime
Time.unscaledTime 替换 Time.time协程
用Time.unscaledDeltaTime 替换 Time.deltaTime
用WaitForSecondsRealtime 替换 WaitForSeconds动画控制器
CullingMode设置为UnscaledTime
如何暂停音频播放
AudioListener.pause = true;当结束暂停时
AudioListener.pause = false;如果想在暂停时继续播放某些音效该咋办?
AudioSource.ignoreListenerPause = true;
模拟代码片段:
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);
}
}边栏推荐
- Mysql version upgrade, copy the Data file directly, the query is very slow
- Eureka Registry
- 【驱动】udev为USB转4串口的每个串口起别名
- Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
- Tcp programming
- Pytorch框架学习记录1——Dataset类代码实战
- After 5 years of Ali internship interview~
- 小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板
- How to Effectively Conduct Retrospective Meetings (Part 1)?
- day10--install mysql on linux
猜你喜欢
Send it to your friends and let TA treat you to fried chicken!

WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template

小程序毕设作品之微信二手交易小程序毕业设计成品(6)开题答辩PPT

智能答题功能,CRMEB知识付费系统必须有!

Small application project works WeChat integral mall small program of graduation design (4) the opening report of finished product

Why is the Kirin 9000 5G version suddenly back in stock?

Pytorch framework learning record 7 - convolutional layer

新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间

小程序毕设作品之微信积分商城小程序毕业设计成品(2)小程序功能

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (6) Question Opening Reply PPT
随机推荐
[Driver] udev sets the owner, group and permissions after GPIO is loaded
Pytorch framework learning record 2 - the use of TensorBoard
Alibaba search new product data API by keyword
小程序毕设作品之微信积分商城小程序毕业设计成品(3)后台功能
防抖与节流
MySQL data query (subtotal and sorting)
Operational configuration: How to run multiple EasyCVR programs as a service in one server?
sublime text 3 settings
小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
Pytorch framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d
Reverse Analysis Practice 2
vscode debugging and remote
How to extract year, month and day data in date type in SQL Server
New LaaS protocol Elephant Swap provides ePLATO with sustainable premium space
Charles 替换 接口响应信息
小程序毕设作品之微信二手交易小程序毕业设计成品(8)毕业设计论文模板
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
How to Effectively Conduct Retrospective Meetings (Part 1)?
mysql structure, index details
How to solve the error "no such file or directory" when EasyCVR starts?