Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page freezes when unloading and reloading Unity instance #115

Open
m-ender opened this issue Jan 22, 2021 · 16 comments
Open

Page freezes when unloading and reloading Unity instance #115

m-ender opened this issue Jan 22, 2021 · 16 comments

Comments

@m-ender
Copy link

m-ender commented Jan 22, 2021

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
Copy link
Owner

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
Copy link
Author

m-ender commented Jan 22, 2021

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

m-ender commented Jan 22, 2021

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
Copy link
Owner

Ok, it's in PR. I'll test it more later this weekend.
#116

@m-ender
Copy link
Author

m-ender commented Jan 25, 2021

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
Copy link
Owner

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

@m-ender
Copy link
Author

m-ender commented Jan 25, 2021

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
Copy link
Author

m-ender commented Jan 25, 2021

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
Copy link
Owner

I do wonder which files you didn't clear

@m-ender
Copy link
Author

m-ender commented Jan 25, 2021

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
Copy link
Owner

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
Copy link
Author

m-ender commented Feb 1, 2021

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
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants