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 110 forks source link

OnXRChange don't work on mobile #303

Closed hugodigio closed 1 year ago

hugodigio commented 1 year ago

Describe the bug I make a code that's listen OnXRChange event to handle some stuff in my scene (ex: disable WebXR Prefab when switch to "normal" mode to use the "normal" camera of my project previously implemented before adding WebXRmodule in it, enabling / disabling some specific behaviour for AR mode ... )

But when I test my code on my mobile device, when I switch to AR mode, the event seems to not been sent ! I've add some Debug.Log messages in my functions that listen this event and no ones are visible in chrome console when switching to AR mode.

I added some debug Button to my UI in Unity to make a Debug.log of the value WabXRManager.Instance.XRState in console, and the value is equal to XR, but no debug.log triggered by event.

To Reproduce Steps to reproduce the behavior:

  1. make basic setup for WebXR export plugin
  2. create a folder for WebXR related script, add asmdef file in it, and give reference of WebXR asmdef file on it
  3. create a script to handle WebXRManager.OnXRChange event

    public class XrSessionHandler : MonoBehaviour
    {
    public UnityEvent OnArModeEnabled;
    public UnityEvent OnVrModeEnabled;
    public UnityEvent OnNoXrModeEnabled;
    public UnityEvent OnWebXrInitialized;
    private bool webXRinitialiazed;
    
    WebXRState currentState;
    // Start is called before the first frame update
    void Start()
    {
        WebXRManager.OnXRChange += XRSessionChange;
    }
    private void XRSessionChange(WebXRState state, int viewsCount, Rect leftRect, Rect rightRect)
    {
        Debug.Log("XrSessionHandler: event from WebXRManager: XRState change");
        XRSessionChange(state);
    }
    
    private void XRSessionChange(WebXRState state)
    {
        Debug.Log("XrSessionHandler: XR state change ! " + state);
        switch (state)
        {
            case WebXRState.VR:
                OnVrModeEnabled?.Invoke();
                break;
            case WebXRState.AR:
                OnArModeEnabled?.Invoke();
                break;
            case WebXRState.NORMAL:
                OnNoXrModeEnabled?.Invoke();
                break;
        }
    }
    }
  4. add this script to an active GameObject in your scene.

Expected behavior On Mobile Web browser, when switch to "AR" button is clicked, Web browser switch to AR mode and WebXRManager.OnXRChange will be triggered.

Unity info:

Smartphone :

hugodigio commented 1 year ago

current bypass I check the WebXRManager.Instance.XRstate value change in the update() function of my script

De-Panther commented 1 year ago

As you mentioned, you disabled the WebXR prefab, where the WebXRManager component is. As it doesn't run (and can't call the update loop), it can't invoke the event.

De-Panther commented 1 year ago

There's a CameraMain game object that you can use for the "normal" mode instead of disabling the WebXR prefab.