On iOS, how do I fade out music when the user presses the home button?

All the well-behaved games I've played on iOS fade out their audio when you push the home button. It's a cool effect, because it lasts until just after the home screen appears, making for a very smooth transition.

From what I have read, Application.Quit(), Application.CancelQuit() and MonoBehaviour.OnApplicationQuit() don't work/aren't called on iOS. This makes it difficult to detect when the user has pushed the home button. By default, my app's music is playing, so it continues to play until just after the home screen appears, and then it cuts off.

How do I make a Unity app handle quitting elegantly?

I had always assumed this was an automatic effect because it seemed ubiquitous among games. Perhaps it's working in this manner because you have the debugger running? Have you tried just running your app on the device without being in debug mode?

OnApplicationPause and OnApplicationFocus may be what you need, they're called when the iphone app gets paused or refocused

Sounds good. I'll check those out.

1 Answer

1

just dieing IS by apples definition quitting elegantly actually. you aren't meant to pull resources when the OS kills you.

Sure, but does Unity just die when the user hits the home button? Music keeps playing until a second or two after the home screen has fully appeared. I assume Unity isn't dying the moment the home button is pushed, but instead letting the home screen appear and then dying?

What are you talking about? I'm talking about reacting to the request to shut down by fading out audio, which sounds perfectly possible from the rest of your comment.