Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Disable, Enable and Reset ArCore

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

  1. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    128
    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:
    128
    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. }