De-Panther / unity-webxr-export

Develop and export WebXR experiences using Unity WebGL
https://de-panther.github.io/unity-webxr-export/
Apache License 2.0
1.04k stars 108 forks source link

Page freezes when unloading and reloading Unity instance #115

Open m-ender opened 3 years ago

m-ender commented 3 years ago

It's normally possible to reload Unity on a running webpage by unloading the current instance (using gameInstance.Quit()) and then loading a new one (using UnityLoader.instantiate as usual) in a callback. However, as soon as I include the webxr.js from this project in the page, this reload completely freezes the page. I suppose there's an issue with the XRManager where it doesn't know how to deal with this program state and doesn't correctly switch over to the new instance. And as far as I can tell, XRManager doesn't expose any methods to shut it down (let alone create a new one, since the type and initWebXRManager methods are not exposed by the module).

Is there an easy way to work around this issue? Otherwise it would be great if the project could support instance reloads out of the box.

De-Panther commented 3 years ago

Interesting.

I'm planning to insert XRManager into the package plugins folder, so it'll be on the same scope as the Unity instance module. Hope it'll help. For now, maybe you can load the Unity instance in an iframe element?

m-ender commented 3 years ago

Do you have a rough timeframe for that change? Just so I can better judge how much effort to put into a workaround.

De-Panther commented 3 years ago

It can be today, and it can be in a few weeks. I'm actually testing this now, but I have some issues with "downgrading" ES6 code to ES5, as Unity use old JavaScript Uglify to minimize code.

De-Panther commented 3 years ago

Seems like I solved my issues. Need to test this on some devices. It'll be this weekend.

I'm not sure if it'll solve your issue, you'll have to try it. I can open a branch if you want to test. (you'll need to use the package from a specific branch on git)

m-ender commented 3 years ago

I'll give it a shot next week. If you're going to release a new version by then anyway, don't worry about a separate branch now.

De-Panther commented 3 years ago

Ok, it's in PR. I'll test it more later this weekend. https://github.com/De-Panther/unity-webxr-export/pull/116

m-ender commented 3 years ago

Thanks for addressing this so quickly. I just updated the package, but unfortunately it seems to have broken WebXR for me. If I try to go into WebXR through the Oculus Browser on the Quest, it just loads indefinitely. If I try it in Chrome (with the Quest connected via Oculus Link), the button doesn't seem to do anything in the browser, but inside the Quest it still seems to try and load something (and if I abort that loading inside the Quest, Chrome crashes).

Your live demo does work for me though. I don't think I changed anything except remove the webxr.js and gl-matrix <script> tags. I'm on 2019.4.11 if that helps.

De-Panther commented 3 years ago

Try to delete the old WebGLTemplates and copy again from the Window > WebXR > Copy WebGLTemplates

m-ender commented 3 years ago

I did, but I'm using a custom template. As far as I can tell from your commits though, all you did was delete those script tags and remove the JS files, which I've done with my custom template as well. I'll give it a spin with one of your templates though to make sure the template is not the issue.

m-ender commented 3 years ago

Alright, after a few more builds and also clearing out all of the old build files instead of just overwriting them, it works again. Sorry for the false alarm. I'll test reloading the Unity instance in a bit and let you know if that works now.

De-Panther commented 3 years ago

I do wonder which files you didn't clear

m-ender commented 3 years ago

Hm, it doesn't look like that fixed it. The page still freezes when quitting and reloading the Unity instance. I'm not sure to what degree the JS plugin's lifetime is actually scoped to the Unity instance's. I'll look into this further tomorrow and see if adding a bit of a delay might help for it to clean up everything, but it might be helpful if the WebXR plugin either shut itself down when Unity quits, or could listen to an event to shut itself down manually.

De-Panther commented 3 years ago

My guess is that although the code is now scoped in the Unity Instance Module, it's still listening and dispatching to the document custom events. But now that all the code is in the same scope, there's no need to use those custom events.

You can look at https://github.com/De-Panther/unity-webxr-export/blob/master/Packages/webxr/Runtime/Plugins/WebGL/webxr.jslib and https://github.com/De-Panther/unity-webxr-export/blob/master/Packages/webxr/Runtime/Plugins/WebGL/webxr.jspre and look for document.addEventListener.

m-ender commented 3 years ago

Are you planning to make these changes to the plugin itself? Unfortunately, I currently don't have the bandwidth to understand the code well enough to make these changes myself and be sure I didn't break anything.

De-Panther commented 3 years ago

I plan to do it, but it'll take time. Also, following Unity WebGL forum, it seems that some versions of Unity have issues with cleaning after Quit. https://forum.unity.com/threads/quit-and-memory-cleanup.571210/page-2#post-6805166 And in general it seems like an interesting thread to read about Unity WebGL and memory cleanup on Quit.

De-Panther commented 3 years ago

I opened a thread in the Unity WebGL forum about that https://forum.unity.com/threads/properly-cleaning-js-memory-on-quit.1078856/