How to reset or restart the ARCore session?

Viewed 4076

I need to reset/restart the ARCore session. In ARKit, I just have to create a new configuration and execute the RunWithConfigAndOptions method, but I can't find any information on how to do this in ARCore. The following is the code I use in Unity for ARKit:

ARKitWorldTrackingSessionConfiguration config = new ARKitWorldTrackingSessionConfiguration();
config.planeDetection = UnityARPlaneDetection.Horizontal;
config.alignment = UnityARAlignment.UnityARAlignmentGravity;
config.enableLightEstimation = true;  

UnityARSessionNativeInterface.GetARSessionNativeInterface().RunWithConfigAndOptions(config, 
                                                                                    UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | 
                                                                                    UnityARSessionRunOption.ARSessionRunOptionResetTracking);

I'm working in Unity, but I guess any information will be useful.

Thanks

3 Answers

Try DestroyImmediate(session) or Destroy(session). One of them may work.

ARCoreSession session = goARCoreDevice.GetComponent<ARCoreSession>();
ARCoreSessionConfig myConfig = session.SessionConfig;

DestroyImmediate(session);
// Destroy(session);

yield return null;

session = goARCoreDevice.AddComponent<ARCoreSession>();
session.SessionConfig = myConfig;
session.enabled = true;

Hope this helps.

For disabling the ARCore session,

GameObject.Find ("ARCore Device").GetComponent<ARCoreSession> ().enabled = false;

For resetting the ARCore session,

//Resetting ARCoreSession
ARCoreSession session = GameObject.Find ("ARCore Device").GetComponent<ARCoreSession> ();
ARCoreSessionConfig myConfig = session.SessionConfig;
DestroyImmediate (session);

In my case, I inherited a Unity3D ARCore project a bit weirdly setup.

The easiest solution in my case, in order to correctly reset the session (otherwise it would not restart tracking on scene load) was to first find the ARCore Device gameobject and uncheck the script enabled in the Unity3D editor.

Once that was done, I just needed to find the ARCoreSession in the Awake() function (but you may need it to do that in Start(), depending on how your project is setup if you have other pre-conditions) and then simply enable the script

GoogleARCore.ARCoreSession g = GameObject.Find("ARCore Device").GetComponent<GoogleARCore.ARCoreSession>();
g.enabled = true;
Related