Phone ringing / home button pressed - save game

Hey

We are wrapping up our first Unity iPhone game, and the question comes up about saving the game when the phone rings and the game shuts down.

From the early days of Unity iPhone there was no hook or callback to catch this.

Has this changed lately, or are you bridging down to the obj-c code to handle it - or do you simply ignore it as something not possible with Unity?

Thanks for sharing your experience in this

/Thomas

I use PlayerPref.SetString to save the actual state of the game. Put it in OnApplicationQuit() and your game will be saved when you quit your app.
I Build 2 strings that i save this way. One contains the world information and one the specific game information like points, level task… And when the game gets load we parse the string back.

For complex game worlds that are dynamic created it would be good to create the world by an key. This way you only have to save the key and not every GameObject to rebuild the Gameworld.

We offer an resume Button if the game was not finished and Load it then with PlayerPref.GetString. So the Player can pause the game to if he wants to take a break and is also able to resume the game after a phone call.

You only need to handle application termination, which calls OnApplicationQuit() before the app terminates.

Damn - almost too easy. So easy I didnt expect it to be “the usual callbacks” as in regular Unity.

Thanks!

/Thomas

Just wondering, how do you use this OnApplicationQuit function? Do you have to put it in a script that sits in every scene that the player might quit from?
P.

You could create an empty gameobject that never gets destroyed, and place it in the beginning of your game. (like scene 0). Then just attach the OnApplicationQuit() script.

Oh yeah, that makes sense! Thanks!
Hey one other question, is there any way of testing this inside unity? I’m guessing you would have to build it to see if it works.

Nope…PlayerPrefs work the same in the editor as in a build, and as for OnApplicationQuit, the docs (remember those? :wink: ) say, “In the editor this is called when the user stops playmode.”

–Eric

oh crap sorry :sweat_smile:
Thanks for that