Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question When Unload (Application.Quit) is Necessary

Discussion in 'Web' started by MasterControlProgram, Oct 11, 2023.

  1. MasterControlProgram


    Apr 18, 2015
    Hi there! I have a general question related to when to manually unload Unity or call Application.Quit().

    Let's say for example I have the following pages in a site:
    • Home - a non unity page to select a game
    • Game - a unity page
    Do I need to handle unload for these following scenarios?
    • Game -> Home (via the browser's back button)
    • Game -> Home (via an in-game "Back to Home" button)
    • Game -> Game (via the browser's refresh button)
    Or put more simply - do I need to manually unload through a page transition? Or would I only need to unload Unity manually if this site was, let's say, a single page that handled multiple unity players on the same page.

    Details on my specific implementation:
    - Next.JS React Site (in non-SSR mode)
    - Using the react-unity-webgl helper
    - Game is pretty tiny in memory
  2. CodeSmile


    Apr 10, 2014
    There is nothing you need to handle when another URL is opened in the same tab, or the tab is reloaded.

    I'm thinking Quit() doesn't even do anything on the web. You'd have to "quit" on the JS side by removing the content iframe Unity renders to - or just plain simply load a different URL, at which point all page resources are unloaded, be it HTML, images, scripts or Unity.
  3. Armynator


    Feb 15, 2013
    Not if it's a separate page. The browser should unload all page contents itself after leaving it.

    Yes. But this should be done on the website through JavaScript, not inside Unity.

    Keep in mind that Application.Quit() does nothing it WebGL. Same with OnApplicationQuit.
    If you'd need OnApplicationQuit for cleanup, such as disconnecting a websocket for example, you'd have to pass a browser event such as window.beforeunload through JavaScript into the Unity player instance.
    The same should work the other way around, with a quit function getting passed into the browser window. You could unload the player instance manually that way then, but implementing it is totally up to you.