如何卸载 unity WebGL

How to Unload unity WebGL

我正在使用 unity webGL 构建,这是在 javascript 中 initalize/load unity webgl 的方式。

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

但是没有办法卸载webgl。我可以删除 canvas 标签,但问题是一些资源保留在内存中,没有被垃圾收集。另外当我尝试删除

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

然后我们得到 furstum 错误

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

有什么合适的方法可以卸载 webgl 吗?

在Unity 2019 beta中,unity提供了一个内置方法(C#/JavaScript)来卸载Unity WebGL and cleanup memory

  • C#:调用 Application.Quit()
  • JS:调用unityInstance.Quit(回调)

您可以使用 C# 版本或 Javascript:

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

有关详细信息,请查看 unity Forums and unity bug。

我在 unity 2019.1.0 beta 中使用过这个函数,但我不确定它是否会清理内存,但它肯定会关闭单元 webgl 应用程序。