Search Unity

Disable, Enable and Reset ArCore

Discussion in 'AR' started by danielesuppo, Jul 27, 2018.

  1. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    Hello,
    I'm working on a geo-referenced app, to discover some AR placeholders in several locations.
    To preserve the battery I think to disable ARCore while the player is just walking to the new location (but the app must stay open because there's a map in it that show the placeholders positions).

    1) So to disable ARcore I use -- arcoreSession.enabled = false;

    2) Next. to enable it again I use -- arcoreSession.enabled = true;

    3) At the same time I have to reset AR camera position (to 0,0,0) and orientation, so I use -- LifecycleManager.Instance.ResetSession();

    My issue is that this is working only once, the 2nd time I get the error "Trying to access a session that has already been destroyed"

    Is there a way to safely reset the ARCore session, so to be able to repeat this workflow as many times as needed?

    Thank-you!
     
  2. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    If someone is interested I've made this code and it seem to work as expected

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ARCoreSessionManagement : MonoBehaviour {
    6.  
    7.     [SerializeField] GameObject arCoreSessionPrefab;
    8.     private GameObject newArCoreSessionPrefab;
    9.     private GoogleARCore.ARCoreSession arcoreSession;
    10.  
    11.  
    12.     private void Start()
    13.     {
    14.         newArCoreSessionPrefab = Instantiate(arCoreSessionPrefab, Vector3.zero, Quaternion.identity);
    15.         arcoreSession = newArCoreSessionPrefab.GetComponent<GoogleARCore.ARCoreSession>();
    16.         arcoreSession.enabled = true;
    17.     }
    18.  
    19.  
    20.  
    21.     public void Reset()
    22.     {
    23.         StartCoroutine(CreateANewSession());
    24.     }
    25.  
    26.     IEnumerator CreateANewSession()
    27.     {
    28.         //Destroy
    29.         arcoreSession.enabled = false;
    30.         if (newArCoreSessionPrefab != null)
    31.             Destroy(newArCoreSessionPrefab);
    32.  
    33.         yield return new WaitForSeconds(1);
    34.  
    35.         //Create a new one
    36.         newArCoreSessionPrefab = Instantiate(arCoreSessionPrefab, Vector3.zero, Quaternion.identity);
    37.         arcoreSession = newArCoreSessionPrefab.GetComponent<GoogleARCore.ARCoreSession>();
    38.         arcoreSession.enabled = true;
    39.     }
    40. }
     
    ARnCo, jmackenzie68, Kalisser and 2 others like this.