Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
2.98k stars 1.11k forks source link

How to correctly reload / restart an AR scene in AR Foundation 5.0.6 including XR Simulation in the Editor #1086

Open michelesandroni opened 1 year ago

michelesandroni commented 1 year ago

How do I correctly restart an AR scene in AR Foundation 5.0.6 using the scene manager? In other words I would like to reload the current active scene and also have all AR planes removed and the tracking session reset.

I know this has been asked before but after searching this repo and in unity forums I still came up with lots outdated solutions that don't seem to work consistently on either iOS or Android. I also couldn't find this clearly explained in the AR Foundation documentation.

I'm also using XR Simulation in the Editor on Windows which stops working after the scene is reloaded.

public void Restart() {
arSession.Reset(); // this is a reference to an ARSession object from the scene
LoaderUtility.Deinitialize();
LoaderUtility.Initialize();
SceneManager.LoadScene(0, LoadSceneMode.Single);
}
VladAtWork-today commented 11 months ago

Did you try creating a button, then on click() event drag and drop your AR Session then call from AR Session-> Reset() method?

michelesandroni commented 11 months ago
  1. I did, the code above is fired when clicking a button
  2. The question specifically asks about the XR simulation in the editor.
dorkbot commented 9 months ago

Did you ever find a solution? I also can not figure out how to completely restart an AR session... https://github.com/Unity-Technologies/arfoundation-samples/issues/1107

makakaorg commented 6 months ago

By Adding SceneUtility.cs & SceneUtilitySetup.cs from this repo to my project, I could get rid of the error when switching scenes in the Editor: "MissingReferenceException: The object of type 'Camera' has been destroyed but you are still trying to access it."

My Scene is loaded 2nd time successfully, and shows the simulated environment, cloud points are shown upon Plane Detection, but Planes are not recognized... They can be recognized in a random moment after randomly moving in the scene after 15, 20, 60... seconds.

I have no issues on Mobiles, only in the Editor.

So, the issue is the same for 2nd, 3rd, so on reloading of the scene.

P.S. I use the Default Simulated Environment for the testing.

ankur-unity commented 4 months ago

@makakaorg - We recently fixed a bug in 5.1.2 patch version which should fix the plane detection issue that you are seeing.

Fixed an issue where calling ARSession.Reset could cause XR Simulation plane detection to fail to discover planes for a period of time before resuming.

https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.1/changelog/CHANGELOG.html#fixed

robertdoehler commented 2 months ago

I struggled with this issue as well. I wonder till today, why there is no easy solution to put all AR stuff into a scene and load it during runtime (and undload/reload the scene if neccessary). <- for the simulated environment in Unity Editor I came up with this solution:

    public void StartSession()
    {    
        XRGeneralSettings instance = XRGeneralSettings.Instance;
        if (instance != null)
        {
            StartCoroutine(instance.Manager.InitializeLoader());
            instance.Manager.StartSubsystems();
        }
    }

    public void EndSession()
    {        
        XRGeneralSettings instance = XRGeneralSettings.Instance;
        if (instance != null)
        {
            instance.Manager.DeinitializeLoader();
            instance.Manager.StopSubsystems();
        }
    }

I call StartSession before I load an AR scene. And I call EndSession before I unload an AR scene. This way I have no issues with camera nullreference exceptions, Object disposed issues, ... in both, editor and Android. I am using AR Foundation 5.1.3