首页 > 解决方案 > 如何卸载统一的 WebGL

问题描述

我正在使用统一 webGL 构建,这是在 javascript 中初始化/加载统一 webgl 的方法。

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

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

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

然后我们得到了 furstum 错误

视锥体之外的屏幕位置(屏幕位置 0.000000、0.000000、0.000000)(相机矩形 0 0 0 0)

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

标签: c#unity3dunity-webgl

解决方案


在 Unity 2019 beta 中,unity 提供了一个内置方法(C#/JavaScript)来卸载Unity WebGL 和清理内存

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

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

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

有关更多详细信息,请查看统一论坛统一错误

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


推荐阅读