Search code examples
c#unity-game-engineunity-webgl

How to Unload unity WebGL


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?


Solution

  • 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.