Enable XR when switching to an AR scene

Hello!

I have a project where I need to create a 2D scene minigame that switches to an AR scene minigame and finally to a 3D VR google cardboard minigame.

I can activate XR for the VR game succesfully with this method found in the documentation:

public IEnumerator StartXRCoroutine()
{
Debug.Log(“Initializing XR…”);
yield return XRGeneralSettings.Instance.Manager.InitializeLoader();

if (XRGeneralSettings.Instance.Manager.activeLoader == null)
{
Debug.LogError(“Initializing XR Failed. Check Editor or Player log for details.”);
}
else
{
Debug.Log(“Starting XR…”);
XRGeneralSettings.Instance.Manager.StartSubsystems();
}
}

But my problem is that this same method does not work for the AR scene.

This AR scene works perfectly if launched as the first scene and with “Initialize XR on Startup” ON.
But when switching from the 2D scene (and XR is not initialized) the method above, that works for initializing XR for the VR scene, does not work for this AR scene.

What am I missing here?

Thanks for any help!

Unity does not support Google Cardboard, so I’m not sure how this is implemented. Based on your description though, it seems like the active XR Loader when your app starts is the Google Cardboard loader. This is why InitializeLoader doesn’t work to initialize ARCore – you still have the Google Cardboard loader active.

Your app is going to have to dig deeper into XR Plug-in Management and switch between multiple loaders while the app is running. Docs for this are here: Advanced configuration for plug-in management | XR Plugin Management | 4.4.1

Thanks a lot for your answer!

I tried this to try and get this AR scene to work properly based on the Documentation you showed me:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Management;

public class ARCoreLoaderManager : MonoBehaviour
{
void Start()
{
ModifyLoaderList();
}

void ModifyLoaderList()
{
var generalSettings = XRGeneralSettings.Instance;
if (generalSettings == null)
{
Debug.LogError(“XRGeneralSettings instance is null”);
return;
}

var settingsManager = generalSettings.Manager;
if (settingsManager == null)
{
Debug.LogError(“XRManagerSettings instance is null”);
return;
}

var readonlyCurrentLoaders = settingsManager.activeLoaders;

// Create a new list and prioritize ARCore
var reorderedLoadersList = new List<UnityEngine.XR.Management.XRLoader>();

foreach (var loader in readonlyCurrentLoaders)
{
if (loader.GetType().Name.Contains(“ARCoreLoader”))
{
// Insert ARCoreLoader at the head of the list
reorderedLoadersList.Insert(0, loader);
}
else
{
// Insert other loaders at the back of the list
reorderedLoadersList.Add(loader);
}
}

// Set the reordered loader list
if (!settingsManager.TrySetLoaders(reorderedLoadersList))
{
Debug.LogError(“Failed to set the reordered loader list! Refer to the documentation for additional information!”);
}
else
{
Debug.Log(“Successfully set the reordered loader list with ARCore prioritized.”);
}
}
}

It didn’t work tho…

Im new to Unity and coding so I don’t know if there is anything else on that doc that I should try…

I must say one thing tho, the AR scene problem is it does not show the camera feed as the background (only when starting it as first scene as I said originaly), just a black screen…, in any case that this detail rings any bells for you that can help me, hehe.
Otherwise the game works and the camera moves with the phone movement.

Again, thanks a lot for trying to help and being a nice person!
Cheers

You should be looking to Deinitialize the ARCore XRLoader, change the active loader, then Initialize the new loader.