How do I clean up and unload a Unity WebGL canvas when changing page

依然范特西╮ 提交于 2019-12-06 07:18:07

问题


I've got a problem on my web application when I am changing the current page after downloading and running a Unity WebGL game.

In fact, I know for sure that the game is running because I continuously hear the sounds when my player is dying.

Is there a way to delete all of the context when changing a web page? Could this be a kind of memory leak?

Edit 1: My web application is done with AngularJS / Material


回答1:


The solution is simple: load the WebGL unity in an iframe, this sandboxes the application.

When you're done with the unity app just remove the node of the iframe like:

var iframe=...;
iframe.parentNode.removeChild(iframe);

And it and all of it's resources should be instantly unloaded.

You can't communicate with the Unity player directly when you're in an iframe but you still can pass messages to and from it with postMessage.




回答2:


Unity 2019.1 provided proper way to Quit the webgl and release the memory(although it has failed in my testing, maybe i am wrong, please let me know it really works or not) :

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

You can use JS version like this

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

For more please check Quit and memory cleanup



来源:https://stackoverflow.com/questions/36738981/how-do-i-clean-up-and-unload-a-unity-webgl-canvas-when-changing-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!