0
\$\begingroup\$

Why does my unity IOS game restart when i press the home button on the device? The same game worked fine on Android device and did not restart on pressing the home button. Thanks

\$\endgroup\$
  • \$\begingroup\$ More info. What do you mean by restart? Do you mean that you lost progress? It's not restarting when you press home, it's closing. So we just need A LOT more info or someone is going to close your question. \$\endgroup\$ – badweasel Jan 29 '15 at 6:17
  • 1
    \$\begingroup\$ The first thing you need to realise is that Android and iOS are different operating systems that handle their apps differently. I think you should read about an app's lifecycle on iOS, to find out what happens. It sounds to me like pressing the home button sets the app's state to "suspended", which could clear it from memory. Also, when it returns to "active", maybe you're actually clearing it yourself with your initialization methods. Anyway, read something like this: developer.xamarin.com/guides/ios/application_fundamentals/… I think you can learn from that. \$\endgroup\$ – Peethor Jan 29 '15 at 6:21
  • \$\begingroup\$ @badweasel Thanks for the reply. If you play the game and then presses the home button to do something else, the game will go into the multitasking tray and basically run in the background until you select the game again which, should then go back to where the player has left the game. Currently when you return to playing the game, the game restarts from the beginning and not where you left off. \$\endgroup\$ – JDolleyArt Jan 30 '15 at 3:23
  • \$\begingroup\$ @Peethor I was used your link and that i believe is the solution to my issue. I will have to do some coding in Xcode to fix it.Thanks for the link!!! \$\endgroup\$ – JDolleyArt Jan 30 '15 at 3:25
  • \$\begingroup\$ I managed to get the game to resume from where i left off after the home button has been pressed on the IOS Device. -> Open Info.Plist in Xcode ->(scroll down to) Required Background modes -> Add by pressing + sign ->(scroll down to) Application does not require background mode -> Change the boolean on the right to 'NO' \$\endgroup\$ – JDolleyArt Jan 30 '15 at 4:17
1
\$\begingroup\$

When the app is in the "multi-task tray" as you call it in the comments, it is NOT running. It is only showing you a screenshot of the last state the game was in before it got suspended.

As Peethor suggests in the comments you do need to understand and learn the life cycle of an iOS app...

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

When the user hits the home button the app will send a message that this is about to happen and then the app will be suspended. (Assuming that it's not a true background app like a music playing app). When you get that message you need to save off the game state. While suspended if the app quits, like say you swipe it up in that background task tray, it will quit without notifying you again.

Then on the reverse of that, if the game is coming out of suspended you'll get another notification. There you can restore this data if necessary and get the game back to where it was. Also if the app just starts up the normal way you could check this as well.

But it's important to realize that generally (in most cases) iOS manages "background" apps by not having them actually running. Which is a very good thing that you must embrace as an iOS developer.

You also might want to look at this: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html

So you could do this:

void OnApplicationPause(bool paused)
{
    if (paused)
    {
      // Save off your game state in case it never comes back
    }
}
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Hi @badweasel thank you for the detailed reply its very informative. I did not that the game wasnt actually runing while it is 'backgrounded'. I basically wanted the game to not reload completely while its suspended or in the multi tasking tray once you select it again. By just changing the bool in Info.plist in Xcode i was able to get it to work :) As the default setting set my game to restart completely every time the home button was pressed! \$\endgroup\$ – JDolleyArt Jan 31 '15 at 7:58
  • \$\begingroup\$ Great to know that's an option inside unity. \$\endgroup\$ – badweasel Jan 31 '15 at 8:22
  • \$\begingroup\$ Its inside of Xcode just open the Info.plist \$\endgroup\$ – JDolleyArt Feb 1 '15 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.