Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Disable, Enable and Reset ArCore

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

  1. danielesuppo

    danielesuppo

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