AR Camera outputs only Black after Unloading other scene.

Hey,
I have trouble with AR Session, XR Session Origin and its Camera when loading new Scenes.

During runtime, I keep a separate scene (call it ARSupport) with these Objects loaded, and I can load additional Scenes fine. The problem is, however, when I unload any other scene than ARSupport, even when it has no relation to it, the camera goes black. I can still see my canvas UI, but the output of the camera is only black. This is present both in XR Simulation and on Android… I am at a loss at what to do.

I am using:

  • Unity Editor 2022.3.13f1
  • ARFoundation 5.1.1

Any help is much appreciated.

Is your project based on AR Foundation samples? We have a script in there that will completely disable AR when a scene is unloaded: arfoundation-samples/Assets/Scripts/Runtime/SceneUtility.cs at main · Unity-Technologies/arfoundation-samples · GitHub

You would want to remove this if this is not your intention.

1 Like

I got it working for Android now, but I will definitely take a look it this. Maybe it solves my problems with XR.Simulation too.

I found that AR was not stopped properly.
Change from

if (arSession != null)
{
     arSession.Reset();
     arSession.enabled = false;
}
UnityEngine.XR.Management.XRGeneralSettings.Instance.Manager.StopSubsystems();
            

to

if (arSession != null)
{
     arSession.Reset();
     arSession.enabled = false;
     if (arSession.subsystem != null)
         arSession.subsystem.Stop();
}
UnityEngine.XR.Management.XRGeneralSettings.Instance.Manager.StopSubsystems();

ReInit AR at Awake

private void Awake()
{
        StartCoroutine(InitXR());
}
IEnumerator InitXR() {
        yield return UnityEngine.XR.Management.XRGeneralSettings.Instance.Manager.InitializeLoader();
        if (UnityEngine.XR.Management.XRGeneralSettings.Instance.Manager.activeLoader != null)
        {
            // Khởi động các subsystem, bao gồm cả AR Camera
            UnityEngine.XR.Management.XRGeneralSettings.Instance.Manager.StartSubsystems();
            // Kiểm tra nếu Camera Subsystem có sẵn
            if (UnityEngine.XR.Management.XRGeneralSettings.Instance.Manager.activeLoader != null)
            {
                Debug.Log("AR Camera Subsystem started successfully.");
            }
            else
            {
                Debug.LogError("No active XRCameraSubsystem is available.");
            }
        }
        else
        {
            Debug.LogError("Failed to initialize XR Loader.");
        }
    }