Search Unity

  1. Unity 2018.3 is now released.
    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. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. 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:
    132
    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:
    132
    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. }