How to quit WebGL build?

Hi everyone,

I’m working on a project that is to be run on a mobile app platform. How it works is that the game is launched from our client’s app and the game is loaded in a WebView. The way users quit the game is to press on the back button. However, we find that the game is still playing music after going back to the app’s home page.

According to our client, the WebView should be loading a blank page when the user navigates back to the app’s home page.

Currently, waiting on a reply if it’s possible to send a message to the unityInstance in the WebView to run unityInstance.Quit(). Anyone have any suggestion on how to unload the game?

Can you share the code?

If it happens all within the same webview the page reload should handle deallocation. If you use the webview only for the Unity app, then it’s likely that the webview didn’t deallocate.

Sorry I don’t have access to their code. From what I gather the WebView is just used for the Unity app. Currently, they are thinking of destroying the WebView when leaving the game.

If you make a Development build using the default template, there should be a reference implementation for unloading the Unity instance fully in the index.html. The code should look like this:

// Unloading web content from DOM so that browser GC can run can be tricky to get right.
// This code snippet shows how to correctly implement a Unity content Unload mechanism to a web page.

// Unloading Unity content enables a web page to reclaim the memory used by Unity, e.g. for
// the purpose of later loading another Unity content instance on the _same_ web page.

// When using this functionality, take caution to carefully make sure to clear all JavaScript code,
// DOM element and event handler references to the old content you may have retained, or
// otherwise the browser's garbage collector will be unable to reclaim the old page.

// N.b. Unity content does _not_ need to be manually unloaded when the user is navigating away from
// the current page to another web page. The browser will take care to clear memory of old visited
// pages automatically. This functionality is only needed if you want to switch between loading
// multiple Unity builds on a single web page.
var quit = document.createElement("button");
quit.style = "margin-left: 5px; background-color: lightgray; border: none; padding: 5px; cursor: pointer";
quit.innerHTML = "Unload";
document.querySelector("#unity-build-title").appendChild(quit);
quit.onclick = () => {
  // Quit Unity application execution
  unityInstance.Quit().then(() => {
    // Remove DOM elements from the page so GC can run
    document.querySelector("#unity-container").remove();
    canvas = null;
    // Remover script elements from the page so GC can run
    script.remove();
    script = null;
  });
};

Hey thanks, this should give them some idea of what to do.