Most of the answer I read on this topic say to use Iframe, but do to the nature of the application we can’t do that. Reloading the whole page is also not an option. I know there is Application.Quit() and instance.Quit() but non of them work. We use Unity 2021.3, but our goal is to update to Unity 6. I know 6 added new stuf to web and that is more optimized in general. i was wondering did it maybe also fixed this issue of killing WebGl instance.
why do you want to do that?
Hi. Take the Unload button implementation from the HTML file of a the Development build made with the default template and put it into a Release build of you project, that should do the trick.
What do you mean under the unload button? What functions does it use?
My app is integrated in a one page website. So I can’t reload the page and every time I reset the app old one stays in memory and just keeps increasing the browser memory witch hurts the performance.
The code in question 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;
});
};
You can find it from
“C:\Program Files\Unity\Hub\Editor\6000.1.0b1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\WebGLTemplates\Base\Default\index.html” but it’s best to make an actual build to make sure you get the correct code.
I tryed to use unityInstance.Quit() before but it would just froze the pace, maybe there is additonal code here that can help I will try it
Ok this work in case I have a mostly empty scene. My app works in a way I load asset bundles from server and as soon as I load the assets and try this my whole page freezes
Ok I found a workaround where I first unload all the scenes and than use Application.Quit() from c# script. Now the problem is it seams that memory is not being released from the browser still.
unityInstance.Quit()
can cause freezes (crashes the engine) if the game’s shutdown code is not meticulously crafted. If you can produce a minimal repro case that crashes the engine, and file a bug report, we can take a look. For instance, a while ago I fixed one crash bug during Quit
that was related to System.Threading
(these should be no-ops on Web and not cause crashes).
Crash isn’t a problem anymore I manage to fix that. Now my problem is that browser doesn’t release memory after I shut down the Unity app. So If I start it again it just adds new memory on top of it
OK, and you are running a Release build? Development build has currently a bug (regression in Emscripten) that prevents all of the memory to be released but it will get fixed eventually once we update Unity to use newer Emscripten version.
Yeah it is an release WebGl Build