Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
michelesandroni opened this issue Jun 24, 2023 · 6 comments
Labels
how to Explains how to accomplish something

Comments

@michelesandroni
Copy link

michelesandroni commented Jun 24, 2023

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);
}
@michelesandroni michelesandroni added the how to Explains how to accomplish something label Jun 24, 2023
@VladAtWork-today
Copy link

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

@michelesandroni
Copy link
Author

  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
Copy link

dorkbot commented Sep 27, 2023

Did you ever find a solution? I also can not figure out how to completely restart an AR session... #1107

@makakaorg
Copy link

makakaorg commented Dec 13, 2023

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
Copy link
Collaborator

@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
Copy link

robertdoehler commented Apr 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
how to Explains how to accomplish something
Projects
None yet
Development

No branches or pull requests

6 participants