Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug ARFoundation not working after loading it programmatically by index

Discussion in 'AR' started by LuisJavierLopez, Sep 1, 2021.

  1. LuisJavierLopez

    LuisJavierLopez

    Joined:
    May 23, 2019
    Posts:
    8
    Unity: 2020.3.14f1

    ARFoundation: 4.1.7 (base project from https://github.com/Unity-Technologies/arfoundation-samples/tree/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 https://github.com/Unity-Technologies/arfoundation-samples/tree/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.

    Code (CSharp):
    1. public void StartXR(int loaderIndex, Action<bool> onFinish = null)
    2.     {
    3.         if (m_SelectedXRLoader != null)
    4.         {
    5.             StopXR();
    6.         }
    7.  
    8.         m_SelectedXRLoader = XRGeneralSettings.Instance.Manager.activeLoaders[loaderIndex];
    9.         StartCoroutine(StartXRCoroutine(onFinish));
    10.     }
    11.  
    12.  
    13.  
    14.  
    15.     IEnumerator StartXRCoroutine(Action<bool> onFinish)
    16.     {
    17.         Debug.Log("*** Init XR loader: " + m_SelectedXRLoader);
    18.  
    19.         var initSuccess = m_SelectedXRLoader.Initialize();
    20.         if (!initSuccess)
    21.         {
    22.             Debug.LogError("*** Error initializing selected loader.");
    23.             onFinish?.Invoke(false);
    24.             yield break;
    25.         }
    26.  
    27.         Debug.Log("*** Start XR loader");
    28.         var startSuccess = m_SelectedXRLoader.Start();
    29.         if (!startSuccess)
    30.         {
    31.             Debug.LogError("*** Error starting selected loader.");
    32.             m_SelectedXRLoader.Deinitialize();
    33.             onFinish?.Invoke(false);
    34.             yield break;
    35.         }
    36.  
    37.         onFinish?.Invoke(true);
    38.     }

    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.SubsystemLifecycleManager`3:GetActiveSubsystemInstance()
    W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager`3: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.SubsystemLifecycleManager`3:GetActiveSubsystemInstance()
    W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager`3:EnsureSubsystemInstanceSet()
    W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager`3: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.SubsystemLifecycleManager`3:GetActiveSubsystemInstance()
    W/Unity : UnityEngine.XR.ARFoundation.SubsystemLifecycleManager`3:EnsureSubsystemInstanceSet()
     
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,816
  3. megatonmedia

    megatonmedia

    Joined:
    May 9, 2015
    Posts:
    15
    @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


     
  4. LuisJavierLopez

    LuisJavierLopez

    Joined:
    May 23, 2019
    Posts:
    8
  5. megatonmedia

    megatonmedia

    Joined:
    May 9, 2015
    Posts:
    15
    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