我的问题如下:目前我在 Android/iOS 游戏中运行了几个协程,但是当我将游戏发送到后台以便用手机尝试其他操作时,这些协程会停止,只有在我返回游戏后才恢复;有什么办法可以让协程在游戏在后台时继续运行吗?
Android 将按设计暂停您的应用程序 https://developer.android.com/training/basics/activity-lifecycle/index.html. 协同例程与您的更新在同一线程中运行 http://unitypatterns.com/introduction-to-coroutines/因此,在前台运行方面区分两者的可能性不大。话虽如此,有一些方法可以解决这个问题。你可以建立一个plugin http://docs.unity3d.com/Documentation/Manual/Plugins.html与 android 平台对话或使用应用程序暂停时 http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnApplicationPause.html并计算应用程序中经过的时间,并在这段时间内执行您想做的任何事情。第一个要求你做一些类似的事情构建一个插件 http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html第二个使用以下内容
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.
}
}
欲了解更多信息,请参阅this http://forum.unity3d.com/threads/117723-Application-runInBackground-is-not-working-on-Android or this https://stackoverflow.com/questions/19163179/how-to-prevent-android-from-killing-my-unity-app.
另请注意构建插件需要专业许可证 http://docs.unity3d.com/Documentation/Manual/Plugins.html
其他协同例程资源
-
协同例程执行顺序 http://answers.unity3d.com/questions/256785/coroutine-start-and-update-execution-order.html
- 协同例程调度程序 http://wiki.unity3d.com/index.php?title=CoroutineScheduler
- 协程深入讲解 http://unitygems.com/coroutines/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)