ARFoundation not working after loading it programmatically by index

Unity: 2020.3.14f1

ARFoundation: 4.1.7 (base project from GitHub - Unity-Technologies/arfoundation-samples at 4.1)

Reproduced: on Android

Problem:
Automatic XR loading for specific XR loaders does not work for ARFoundation.

How to reproduce

This is the Unity project: https://manyworlds.synology.me/manyworlds/development/tmp/arfoundation-samples-progrmmatically.zip

  1. Clone GitHub - Unity-Technologies/arfoundation-samples at 4.1
  2. Build scene TogglePlaneDetection to test that it works.
  3. Disable “Project Settings > XR Plug-in Management > Initialize On Start-Up”.
  4. Load XR plugin at index 0 programmatically (snippet bellow).
  5. Result: black screen, so no plane detection.
    *Expected: the camera should switch on and detected planes should appear on the environment.
public void StartXR(int loaderIndex, Action<bool> onFinish = null)
    {
        if (m_SelectedXRLoader != null)
        {
            StopXR();
        }

        m_SelectedXRLoader = XRGeneralSettings.Instance.Manager.activeLoaders[loaderIndex];
        StartCoroutine(StartXRCoroutine(onFinish));
    }




    IEnumerator StartXRCoroutine(Action<bool> onFinish)
    {
        Debug.Log("*** Init XR loader: " + m_SelectedXRLoader);

        var initSuccess = m_SelectedXRLoader.Initialize();
        if (!initSuccess)
        {
            Debug.LogError("*** Error initializing selected loader.");
            onFinish?.Invoke(false);
            yield break;
        }

        Debug.Log("*** Start XR loader");
        var startSuccess = m_SelectedXRLoader.Start();
        if (!startSuccess)
        {
            Debug.LogError("*** Error starting selected loader.");
            m_SelectedXRLoader.Deinitialize();
            onFinish?.Invoke(false);
            yield break;
        }

        onFinish?.Invoke(true);
    }

Attempts

  1. Without changing anything in the scene, just adding the manual XRLoader code, doesn’t work → black screen.
  2. Delaying ARSession and ARSessionOrigin activation after the XR plugin is loaded successfully → black screnn; there is an infinite loop because ARSession.state = None.
  3. Loading the XR plugin in a scene and changing to the plane detection scene 1 second later (just in case) → black screen and log bellow

No active UnityEngine.XR.ARSubsystems.XRSessionSubsystem is available. Please ensure that a valid loader configuration exists in the XR project settings.
W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager3:GetActiveSubsystemInstance()* *W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager3:EnsureSubsystemInstanceSet()
W/Unity : UnityEngine.XR.ARFoundation.ARSession:OnEnable()
W/Unity :
W/Unity : No ARSession available for the current platform. Please ensure you have installed the relevant XR Plugin package for this platform via the Package Manager.
W/Unity :
W/Unity : No active UnityEngine.XR.ARSubsystems.XRCameraSubsystem is available. Please ensure that a valid loader configuration exists in the XR project settings.
W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager3:GetActiveSubsystemInstance()* *W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager3:EnsureSubsystemInstanceSet()
W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager3:OnEnable()* *W/Unity :* *W/Unity : No active UnityEngine.XR.XRInputSubsystem is available. Please ensure that a valid loader configuration exists in the XR project settings.* *W/Unity : UnityEngine.XR.ARFoundation.ARInputManager:GetActiveSubsystemInstance()* *W/Unity : UnityEngine.XR.ARFoundation.ARInputManager:OnEnable()* *W/Unity :* *W/Unity : No active UnityEngine.XR.ARSubsystems.XRPlaneSubsystem is available. Please ensure that a valid loader configuration exists in the XR project settings.* *W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager3:GetActiveSubsystemInstance()
W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager`3:EnsureSubsystemInstanceSet()

Could you file a bug report for us? Our QA team can then have a look. Unity QA: Building quality with passion

@LuisJavierLopez DId you create a bug in the Unity Issue tracker? If so, could you please post a link to the bug? I’m dealing with this issue and this is critical to making an XR app. Quite frustrating

Bug report: https://fogbugz.unity3d.com/default.asp?1380981_35q7d9fleng1l6tj

Digging into this myself, I found for the system to start up XRGeneralSettings.Instance.Manager.activeLoader must be set to the loader we just started with XRLoader.Start(). There is no way for the end user to set this value. It’s Set Private inside a sealed class. activeLoader is only set in two places in the code. InitializeLoader() and InitializeLoaderSync(). Both if which will error out when called after XRLoader.Start()

The only way I could get this work was to hack the XRGeneralSettings code. To change activeLoader to public set sop I can manually set activeLoader.

It looks like the Unity supplied code sample has never worked. Either it’s broken and untested, or the instructions are missing an important step.

I tested on Adroid using: ARFoundation 4.1.7 Unity 2021.1.28 on Android

1 Like