4

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

6
  • This is Unity bug for sure. However, you can create a browser script to force unload everything that's loaded. Apr 20 '16 at 9:55
  • The thing is that I don't really know what is loaded by Unity. How do I manage to do that without unloading my own scripts ?
    – MadJlzz
    Apr 20 '16 at 10:01
  • 1
    There are like 5-10 files generated by Unity, unload them all (YourBuildName.data, YourBuildName.html.mem, YourBuildName.js, etc.) Edit: I think the are in Release or Debug folder depending on a build Apr 20 '16 at 10:13
  • What do you mean by unloading them ? In the index.html file I clearly see that a unity script is loading the files but I am not having any access to tell Unity to unload this files.
    – MadJlzz
    Apr 20 '16 at 10:33
  • @NikaKasradze: javascript doesn't have any direct way to remove non trivial resources. When something is loaded it usually stays that way. Apr 21 '16 at 9:06
3

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.

10
  • Thanks for you answer. Your solution looks pretty nice and is interreseting but as soon as I put my unity WebGL inside an iframe, it doesn't even look for loading ressources...
    – MadJlzz
    Apr 22 '16 at 11:33
  • This should not happen, can you give some more info? Web development console logs, online demo with the issue etc? Apr 22 '16 at 11:37
  • Well, I answer to quick. In fact, with the raw index.html that Unity generates when you build your game, it's working (and the datas are unloaded without doing anything). The thing is that I was trying to pass AngularJS values directly through the iframe. I think I have no other solution to use the old school GET method. Or is it possible to use postMessage to pass AngularJS data's ?
    – MadJlzz
    Apr 22 '16 at 11:54
  • 1
    It should be possible, but if you plan on passing this data only on init, the best way is to use the hash portion of the url (the part after # char, ex: http://example.com/path?search#hash). This way it won't even be sent to the server while still being available in JavaScript with location.hash. Apr 22 '16 at 12:00
  • God thanks for you help. You helped me out a lot ! It's ok for me but if you think I need to clarify something for the others, please tell me before I accept your answer :)
    – MadJlzz
    Apr 22 '16 at 12:40
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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