0
\$\begingroup\$

I created a static class CustomPlayerPrefs, it has a WriteToDisk() method that writes the saves to disk.

For saving I am using Application.quiting event:

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
static void Init()
{
    Application.quitting += WriteToDisk;
}

But on android it never works. I know that applications are not closed but paused, but it doesn't even work when I restart the device or force the application to stop.

How to fix this or how to make it better?

Need IOS and Android support.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

To be safe, you actually need to save in three different places, when the game is paused (OnApplicationPause), when the game loses focus (OnApplicationFocus) and finally just in case when the game quits (OnApplicationQuit). The last one should be unnecessary accordingly to some, but better safe than sorry.

public class MyGameManager : MonoBehaviour
{
    // ...

    /// <summary>
    /// Called when the application pauses.
    /// </summary>
    /// <param name="pause"><c>true</c> if the application is paused, else <c>false</c>.</param>
    private static void OnApplicationPause(bool pause)
    {
        if (pause)
            Save();
    }



    /// <summary>
    /// Called when the gamme loses or gains focus. 
    /// </summary>
    /// <param name="hasFocus"><c>true</c> if the gameobject has focus, else <c>false</c>.</param>
    /// <remarks>
    /// On Windows Store Apps and Windows Phone 8.1 there's no application quit event,
    /// consider using OnApplicationFocus event when hasFocus equals false.
    /// </remarks>
    void OnApplicationFocus(bool hasFocus)
    {
        if(!hasFocus)
            Save();
    }



    /// <summary>
    /// Called before the application quits.
    /// </summary>
    /// <remarks>
    /// iOS applications are usually suspended and do not quit.
    /// On Windows Store Apps and Windows Phone 8.1 there's no application quit event,
    /// consider using OnApplicationFocus event when focusStatus equals false.
    /// On WebGL is not possible to implement OnApplicationQuit due to nature of the
    /// browser tabs closing.
    /// </remarks>
    private void OnApplicationQuit()
    {
        // Note that we do not get an OnApplicationQuit event on iOS devices. We
        // therefore saves the data in the OnApplicationPause and OnApplicationFocus
        // events, and have this code just in case:
        Save();
    }
\$\endgroup\$
1
  • \$\begingroup\$ I found on my new phone (Google Pixel 6 Pro) that neither OnApplicationPause nor OnApplicationQuit were called when swiping an app away, so OnApplicationFocus was absolutely the necessary one. But yeah as above I think we have to implement all 3. The pause thing was a surprise, but perhaps this suggests it's "by design": issuetracker.unity3d.com/issues/… \$\endgroup\$ Apr 27, 2022 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .