C# How to wait on callback, should I use threading?

When minimizing my android game I want to save certain data to the cloud. This works fine when using a button (exit button will save and then exit), but to make cheating a little harder I also want to save the game when the player minimizes.

When this happens unity calls: OnApplicationPaused() where I can handle this saving. Unfortunately, the play-games-plugin-for-unity that I use, uses a callback which is called when saving is finished, which means the message pipeline continues during the saving and the app is minimized before saving is finished.

My question is how do I hold the app from minimizing untill my callback is called and the saving is finished.


This is a rephrasing of my previous question here. I think I made the question a bit to specific there to get an answer. Since this is also my last remaining issue I would really love a fix for this as soon as possible!

Saving to the cloud is a pretty difficult thing to do when an app goes to sleep. I’m not as familiar with Android, but on iOS, you have to add in unmanaged code to run in a background thread when the app is put to sleep. There is no way to simply stop an app from going to sleep: imagine the user is receiving an urgent phone call and you say “oh no, I want to save your data first.” That’s why it must happen in some kind of background thread, but that thread is going to have to be managed by the operating system, not Unity. Try to draw a diagram of the saving system you described is currently happening and think of potential pitfalls that can make that diagram stop functioning.

Another option is to save locally when the app goes to sleep. Try pushing to the cloud only when the game is open. I currently play a game that only saves when I tell it to. Putting the game to sleep isn’t super-safe because if the app is killed, I lose my progress. I’m ok with this setup though because in general I would rather find a save point.

Every game is different, but I think you should rule out saving to the cloud when the app goes to sleep because of all the situations possible such as: the user has no internet connection, etc. Try instead to make your game as consistent as possible at coming back into the same state after waking from sleep.