1

I am using unity webGL build and this is the way to initalize/load unity webgl in javascript.

 var gameInstance = UnityLoader.instantiate("gameContainer", "Build/WebGLDemo.json", {onProgress: UnityProgress});

but there is no way to unload the webgl. I can remove the canvas tag but the problem is that some resources are remains in the memory which are not garbage collected. additionally when i try to remove

function DeleteGame(){
            console.log("remove game");
            document.getElementById("gameContainer").remove();
            //gameInstance = null;
      }

then we are getting furstum error

Screen position out of view frustum (screen pos 0.000000, 0.000000, 0.000000) (Camera rect 0 0 0 0)

Is there any proper way available to unload the webgl?

4

2 Answers 2

4

In Unity 2019 beta, unity provides a built-in method(C#/JavaScript) to unload Unity WebGL and cleanup memory.

  • C#: call Application.Quit()
  • JS: call unityInstance.Quit(callback)

You can either use C# version or Javascript:

unityInstance.Quit(function() {
    console.log("done!");
});
unityInstance = null;

For more details check unity Forums and unity bug.

I have used this function in unity 2019.1.0 beta but i am not sure that its cleaup the memory but it definitely close unit webgl application.

1
  • Hi, It says unityInstance.Quit is not a function. I have correctly assigned the createUnityInstance function to unityInstance but It does not work Sep 17, 2022 at 10:59
0

Note that from Unity v2020.1 on, unityInstance.Quit() no longer takes a callback function, but is a Promise instead. So you have to call it like this:

unityInstance.Quit().then(function() {
    console.log("done!");
});
unityInstance = null;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.