unity-webxr-export icon indicating copy to clipboard operation
unity-webxr-export copied to clipboard

DeviceOrientationEvent.requestPermission()

Open psusset opened this issue 4 years ago • 8 comments
trafficstars

Hello,

In order to make this plugin work on iOS, you need to ask user permission for device orientation capture. To do that, we need to insert the following code in the requestSession action.

navigator.xr.requestSession('immersive-vr', { requiredFeatures: thisXRMananger.gameModule.WebXR.Settings.VRRequiredReferenceSpace, optionalFeatures: thisXRMananger.gameModule.WebXR.Settings.VROptionalFeatures }).then(async (session) => { if (typeof(DeviceOrientationEvent) !== "undefined" && typeof(DeviceOrientationEvent).requestPermission === 'function') { DeviceOrientationEvent.requestPermission() .then(response => { if (response === 'granted') { session.isImmersive = true; session.isInSession = true; session.isAR = false; thisXRMananger.xrSession = session; thisXRMananger.onSessionStarted(session); } }).catch(function (error) { thisXRMananger.BrowserObject.resumeAsyncCallbacks(); thisXRMananger.BrowserObject.mainLoop.resume(); }); } else { session.isImmersive = true; session.isInSession = true; session.isAR = false; thisXRMananger.xrSession = session; thisXRMananger.onSessionStarted(session); } });

psusset avatar Jul 21 '21 13:07 psusset

Thanks for the feedback. I don't have an iOS device, so I don't have a way to test it on those.

iOS in general doesn't support WebXR. I think that it does support WebVR, or some version of it with cardboard, but it's all using the WebXR Polyfill. So if the DeviceOrientationEvent is required, better report about it at the WebXR Polyfill repo, as I see you did https://github.com/immersive-web/webxr-polyfill/issues/161

De-Panther avatar Jul 22 '21 21:07 De-Panther

In deed, it works with polyfill on iOS. I test this code on your plugin and it works well on iOS (at least for my purpose - cardboard with reticle). For those who want to support WebXR on iOS with this plugin, you will also need to change the html template so the unity can use all the screen in landscape mode.

psusset avatar Jul 23 '21 05:07 psusset

OMG THANK YOU SO MUCH

GittyUpperNinja avatar Jul 30 '21 01:07 GittyUpperNinja

Hi @psusset , it works for VR button in web site page but not in scene button. How do you use it for in scene button?

jamwitk avatar Feb 14 '22 08:02 jamwitk

HI @jamwitk , I use an HTML button in the page and not a button on the unity side. I think it's not possible to use a button inside of Unity because the deviceorientationrequest must be handled by the webpage. But you can display your html button on top of the Unity canvas to get the same result. Thanks to javascript call, Unity can control the display of the button.

psusset avatar Feb 14 '22 08:02 psusset

Thank you for your feedback, I have tried write into .jslib and call on start but it gives error such that it need user gesture something like this. It seems as you say it is not possible.

mergeInto(LibraryManager.library, {
   CheckForPermission : function (){
       if (typeof(DeviceOrientationEvent) !== "undefined" && typeof(DeviceOrientationEvent).requestPermission === 'function')
       {
           DeviceOrientationEvent.requestPermission().then(
               function(response) {
                   if (response === 'granted') {
                      window.alert("granted 1");
                   }
               }).catch(function (error) {
               window.alert(error);
           });
       }
       else
       {
           window.alert("granted 2");
       };
   },
})

jamwitk avatar Feb 14 '22 10:02 jamwitk

I think that in Unity 2022.1 beta they added gyro support for WebGL. So there should be a way to allow gyro from Unity and not just from the page

De-Panther avatar Feb 14 '22 11:02 De-Panther

Also, if you have relevant device, you should be active in the WebXR Polyfill issue I mentioned and the other relevant issues mentioned there.

De-Panther avatar Feb 14 '22 11:02 De-Panther