Unity coroutines stop while in background

11,134

Solution 1

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.

Also note that building a plugin requires a Pro Licence

Additional Co-Routine resources

Solution 2

You can't make something permanently run in the background. The OS will event eventually pause it. Only VOIP, Media Player or GPS are allowed. Everything else only get an extended time frame until pause.

For iOS you can change "Application does not run in background" in info.plist

Share:
11,134
user3224109
Author by

user3224109

Updated on August 21, 2022

Comments

  • user3224109
    user3224109 over 1 year

    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?

  • user3224109
    user3224109 over 10 years
    But the game IS running in background, its just the coroutines that stops, so there is no way of making the coroutines continue running with the game while on background??
  • Tsukasa
    Tsukasa over 10 years
    By design there is no way to do so. As @Terrance posted you can use OnApplicationPause as a trick to store the state and make your calculations on restoration.
  • user3224109
    user3224109 over 10 years
    Thanks for the answer! a quick follow up question, is there a way of knowing all the coroutines currently running in a game even though they are in different scrips? probably not but its worth asking
  • rutter
    rutter over 10 years
    There's not much built-in coroutine management. There are a few third-party libraries that help.
  • Jerdak
    Jerdak over 10 years
    @user3224109 I have a dumb little hack that I never finished but that might get you part way to coroutine tracking. I've thrown what I have up on Github fwiw.
  • Terrance
    Terrance over 10 years
    I'd also look at the Co-Routine Scheduler I added to the additional resources
  • Alexandru
    Alexandru over 6 years
    @Naeim It says that in the docs, but this is still useful because people might want it in the editor. Unity docs say exactly this: Note: This property is ignored on Android and iOS.