Unity协程在后台停止 [英] Unity coroutines stop while in background

查看:684
本文介绍了Unity协程在后台停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:当前,我在Android/iOS游戏中运行了几个协程,但是当我将游戏发送到后台以尝试使用手机进行其他操作时,协程停止并且仅在返回到游戏;有什么方法可以让协程在后台运行时继续运行?

my question is the following: currently i have several coroutines running in my game for android/iOS but when i send the game to background in order to try other things with the phone those coroutines stop and only resume after i get back to the game; is there any way to make the coroutines continue running while the game is in background?

推荐答案

Android将按设计暂停您的应用程序. 协同例程与您的更新程序在同一线程中运行,因此在术语上区分了两者在前台运行不太可能.话虽如此,但是有解决方法.您可以构建与Android平台进行对话的插件或使用 OnApplicationPause 并计算在应用程序中经过的时间,然后执行您想做的一切在之间的时间范围内.第一个要求您按照构建插件的方式进行操作 第二个使用以下内容

Android will suspend your application by design. Co-routines run in the same thread as your Updates so making a distinction between the two in terms of running in the foreground is not too likely. Having said that there are ways around this. You could build a plugin that talks to the android platform or use OnApplicationPause and calculate the time passed in the application and do whatever it is you were wanting to do in the time frame between. The first requiring you do something along the lines of building a plugin and the second using the following

public void OnApplicationPause(bool paused) {
    if(paused) {
       // Game is paused, remember the time
    } else {
       // Game is unpaused, calculate the time passed since the game was paused and use this time to calculate build times of your buildings or how much money the player has gained in the meantime. 
    }
}

有关更多信息,请参见

for more info see this or this.

还请注意,构建插件需要专业许可

其他常规程序资源

  • Co-Routine Execution Order
  • Co-Routine Scheduler
  • Deep Explaination of Co-Routines

这篇关于Unity协程在后台停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆