2
\$\begingroup\$

Unity has messages called OnApplicationPause and OnApplicationFocus. They are called on each MonoBehaviour as the user exits/suspends the application.

I have successfully used this to save data in a regular build, but the save never happens when I am in a development build. My guess is that it is doing some extra logging in development builds when the application exits, and runs out of time before the save can occur.

This is the code I am using to save:

#if UNITY_IOS || UNITY_EDITOR
    private void OnApplicationFocus(bool focus)
    {
        if (focus)
        {
            EnterApplication();
        }
        else
        {
            ExitApplication();
        }
    }
#endif

#if UNITY_ANDROID && !UNITY_EDITOR
    private void OnApplicationPause(bool pause)
    {
        if (pause)
        {
            ExitApplication();
        }
        else
        {
            EnterApplication();
        }
    }
#endif

private void ExitApplication()
{
    SaveData();
}

I am saving my data using a C# FileStream, not PlayerPrefs.

As you have noticed, I am not using OnApplicationQuit. This is because I want the application to save as the user backgrounds the app or exits the app. So please keep this in mind before suggesting I use OnApplicationQuit. I also believe the results would be the same in terms of failure.

Is it save to auto-save my game state when these methods are called? Couldn't the operating system take control away before the save completes? If it is not safe, is there a more performant solution than periodically auto-saving?

\$\endgroup\$
5
  • \$\begingroup\$ Have you profiled your save routine to find whether it might run longer than expected in some circumstances? \$\endgroup\$
    – DMGregory
    Apr 17, 2020 at 12:10
  • \$\begingroup\$ @DMGregory No I have not. I will do that, but in my case, the payload size should be identical every time. (I am not saving modularly.) That is a good idea for a test, though - intentially save a large payload on exit. \$\endgroup\$
    – Evorlor
    Apr 17, 2020 at 12:15
  • 1
    \$\begingroup\$ @DMGregory Test proves that it is unreliable to save data in those methods. A simple delay before code execution causes it to fail. Thanks for the tip. \$\endgroup\$
    – Evorlor
    Apr 17, 2020 at 12:23
  • 1
    \$\begingroup\$ Good find! Want to post that as an answer, or amend this question to ask for more reliable alternatives based on your findings? \$\endgroup\$
    – DMGregory
    Apr 17, 2020 at 12:27
  • \$\begingroup\$ @DMGregory Will do - let me finish running my tests. I will probably form a new question given the new information. \$\endgroup\$
    – Evorlor
    Apr 17, 2020 at 12:28

1 Answer 1

1
\$\begingroup\$

No, it is not safe.

You can see this by building in debug mode. The extra time it takes to log data prevents it from calling these methods before the OS takes back control.

One solution is to implement an auto-save feature that saves on a timer and/or key events.

\$\endgroup\$

You must log in to answer this question.

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