Unity provides several event functions to manage the application’s lifecycle:
Called when the app gains or loses focus.
- Input:
bool hasFocus
(true = focused, false = unfocused). - Use Case: Pause or resume real-time processes like music or updates.
Called when the app is paused or resumed.
- Input:
bool isPaused
(true = paused, false = running). - Use Case: Save the game state or pause timers.
Called when the app is about to quit.
- Use Case: Save data or send a shutdown signal to the server.
On desktop platforms (Windows, macOS, Linux), OnApplicationQuit()
is reliably called when the app closes normally.
However, on mobile (iOS, Android):
- The OS can terminate the app at any time to reclaim resources (e.g., swiping the app away, memory cleanup).
- In such cases,
OnApplicationQuit()
may not be called, making it unreliable for critical shutdown logic.
- Use
OnApplicationPause()
for saving data on mobile. - Consider periodic autosaving or real-time syncing to ensure critical data is preserved.