OnApplicationQuit from WebGL

Hello,

I have a script that handles OnApplicationQuit in my game.

in this function we do some things like writing out to PlayerPrefs to save progress.

This doesn’t seem to be called when I refresh my page in Chrome or Firefox, or if it IS being called and I have some sort of exception or something in that script, I can’t seem to find out because the console in both Firefox and Chrome won’t preserve the logs when the page reloads even if I check the “Preserve log” box in the developer tools of my browser.

Do WebGL logs get written to a file somewhere on my hard-drive that I can reference after I’ve closed the game by either refreshing my page, closing my browser, or navigating away from the page?

Or do I have to write some javascript on my webpage to mimic what i’m doing in OnApplicationQuit instead of doing it in c# script in unity?

Any help would be appreciated.

I guess that OnApplicationQuit is not yet supported in WebGL. If you file a bug, I will take a look (write in the bug description that it should be assigned to me).

You can probably fix this yourself by using the window.onbeforeunload event in JavaScript.

Actually, no need to file a bug, already on it.

I was wondering if this ever got addressed. I’m running up against the same issue with my game.

Hello teh_blake.

In WebGL you should always execute PlayerPrefs.Save() during gameplay to ensure the data is saved to the database (for example, each time when user confirms the change). You should not expect PlayerPrefs to be saved when user closes the window (i.e. in OnApplicationQuit etc.), because all indexedDB operations are asynchronous, and there is no reliable way to perform any asynchronous operation when user closes the window. Success of such operation depends on browser implementation and timing, so you should always assume that PlayerPrefs are not saved at the moment when the window is closed.

2 Likes