Search Unity

  1. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

Resolved Reset AR Session

Discussion in 'AR' started by blanx, Jun 22, 2018.

  1. blanx

    blanx

    Joined:
    Feb 26, 2016
    Posts:
    62
    Is there a way to reset the AR session?

    I tried to use this in, but it seems it does not do anything:
    ARSubsystemManager.DestroySubsystems();
    ARSubsystemManager.CreateSubsystems();

    Sometimes the tracking goes wrong or when reopening an app all es messed up and I want to give the users to reset the scene. Then all planes have to removed and the tracking points and so on.
    For ARKit I had a script that can do this, but now with foundation it seems a bit more difficult.
     
  2. jimmya

    jimmya

    Unity Technologies

    Joined:
    Nov 15, 2016
    Posts:
    793
    You should be able to destroy the ARSession GameObject and Instantiate a new one to achieve this.
     
  3. blanx

    blanx

    Joined:
    Feb 26, 2016
    Posts:
    62
    Thanks. Will try that.
    And then it is just a matter of destroying the visual planes to have a clean new session?
     
  4. TijsVdV

    TijsVdV

    Joined:
    Mar 7, 2017
    Posts:
    23
    Did this work for you? I tried this but when but i get this error when enabeling the newly spawned arsession: InvalidOperationException: Cannot start AR session because there is no session subsystem
     
  5. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    237
    Same here, "InvalidOperationException: Cannot start AR session because there is no session subsystem." when enabling ARSession. I also get it if I load from SampleScene to another scene and then try to load SampleScene again.

    Update: Exception only occurs in the Editor.
     
    Last edited: Jun 27, 2018
  6. StefanAugmentors

    StefanAugmentors

    Joined:
    Nov 3, 2017
    Posts:
    15
    Hi. Did destroying the AR Session object and re-instantiating work for anyone? I get a black screen when I do that.
     
  7. WolfBeardedLion

    WolfBeardedLion

    Joined:
    Apr 5, 2013
    Posts:
    27
    I'm also getting a black screen, instead of the camera feed, after destroying the ARSession component and then adding it back to the same gameObject.
     
  8. StefanAugmentors

    StefanAugmentors

    Joined:
    Nov 3, 2017
    Posts:
    15
    I couldn't find a nice way to reset the session. For now I get all the planes and point clouds and destroy/disable them manually.
     
  9. Phoera

    Phoera

    Joined:
    Mar 10, 2015
    Posts:
    9
    If I'm reloading AR scene for the second time, I'm getting the "Cannot start AR session.." error, and device is not tracking any planes, due to session fail
     
  10. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    605
    I just did a test on a Pixel where I enabled/disabled the ARSession component, and also Destroyed and Instantiated it. All seemed to work as expected.

    For this particular issue:
    What device and OS version?
     
  11. Cowcak

    Cowcak

    Joined:
    Mar 11, 2015
    Posts:
    8
    I have the same issue when loading scene again. Just one note that I'm using multiple scenes loaded additively and AR session lives in the scene from scene load.
     
  12. Phoera

    Phoera

    Joined:
    Mar 10, 2015
    Posts:
    9
    I can't already reproduce this exact error, it's been lost in the iterations of fixing attempts.
    The thing is I was trying to fix nonexistent error, because it was only in the Editor, as soon as I built my app for device, everything went okay.
    I used directives for ARSubsystemManager functions, because all of the systems are null in the editor, therefore it can't function properly.

    So it looks like this and works like charm even when reloading scene:

    Code (CSharp):
    1. void Start()
    2.     {
    3. #if !UNITY_EDITOR
    4.         ARSubsystemManager.DestroySubsystems();
    5.         ARSubsystemManager.CreateSubsystems();
    6.         ARSubsystemManager.StopSubsystems();
    7.         arSession.gameObject.SetActive(false);
    8. #endif
    9.     }
    10.  
    11. public async void StartTracking()
    12.     {
    13. #if !UNITY_EDITOR
    14.         arSession.gameObject.SetActive(true);
    15.         ARSubsystemManager.StartSubsystems();
    16.         arPlaneManager.planeAdded += PlaneTracked;
    17. #else
    18.         //here comes some emulation of plane tracking for Editor:
    19.         //PlaceInfinitePlane(Vector3.zero);
    20.         //await new WaitForSeconds(1f);
    21.         //OnPlaneTracked?.Invoke();
    22. #endif
    23.     }
    24.  
    May it help someone to figure out how to handle reload of ar session =)
     
    Last edited: Jul 12, 2018
    WolfBeardedLion likes this.
  13. StefanAugmentors

    StefanAugmentors

    Joined:
    Nov 3, 2017
    Posts:
    15
    Same here. I think multiple scenes is breaking something.
     
  14. headlessstudio

    headlessstudio

    Joined:
    Feb 25, 2016
    Posts:
    83
    We're also having the same issue with reloading the scene a second time and getting a black screen. Samsung S8 and Android, and iPhone 7 Plus and iOS 12
     
  15. FlorentPoittevin

    FlorentPoittevin

    Joined:
    Jun 29, 2019
    Posts:
    2
    Same problem here
     
  16. FlorentPoittevin

    FlorentPoittevin

    Joined:
    Jun 29, 2019
    Posts:
    2
    ismaelnascimentoash and stina95 like this.
  17. azanoni

    azanoni

    Joined:
    Jul 30, 2019
    Posts:
    3
    But how we can call that method? I have a button that calls "ResetDemo" function to load the firstScene and reset the ARKit but doestn works :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using UnityEngine;
    5. using UnityEngine.XR.ARFoundation;
    6. using UnityEngine.XR.ARSubsystems;
    7. using UnityEditor;
    8. using UnityEngine.SceneManagement;
    9.  
    10. public class HumanBodyTracker : MonoBehaviour
    11. {
    12. ....
    13.     private void Reset() {
    14.          Debug.Log("Reseting....");
    15.     }
    16.  
    17.     public void ResetDemo()
    18.     {
    19.         Reset();
    20.         SceneManager.LoadScene("StartScene");
    21.         Debug.Log("Loading Start Scene...");
    22.         message = "Loading Start Scene...";
    23.  
    24.     }
     
  18. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24


    You have to get a reference to the ARSession in your scene to be able to call the Reset(). You can either get it by placing your script on the same GameObject, as the ARSession is on
    Code (CSharp):
    1. ARSession arSession = GetComponent<ARSession>();
    2. arSession.Reset();

    or you can link it by inserting a serialized field and set the reference in the inspector
    Code (CSharp):
    1. using UnityEngine.XR.ARFoundation;
    2.  
    3. [SerializeField]
    4. private ARSession _arSession;
    5.  
    6.  

    and call it in your method as follows
    Code (CSharp):
    1. _arSession.Reset();

    Unfortunately this Reset() doesn't solve the problem with the black screen in my project... So I raise my hand to say: "Same problem here";)
     
    ROBYER1 and nir11 like this.
  19. azanoni

    azanoni

    Joined:
    Jul 30, 2019
    Posts:
    3
    Thanks, works like "its reseting" but same issue, black screen I think that maybe we need to store the gameobjects inside of the session...
     
  20. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    The Reset() works fine, if you don't leave the scene and stay in the AR-scene. As I can see from your posting you are then also loading another scene within your special reset, so this causes the black screen. Comment that out, so your app stays in the same scene and try the reset again.

    The problems stays the same: If you leave an AR-scene and later return to an AR-scene the background is just black :(
     
  21. vincentfretin

    vincentfretin

    Joined:
    Jul 4, 2019
    Posts:
    7
    Hi, I didn't play with multiple scenes yet, so I'm not sure if it's relevant, in chapter 20 of the "Unity AR & VR, by Tutorial" book, they have two scenes, and they add to the ARSession and ARSessionOrigin gameobjects a DontDestroyOnLoad script so those objects are not destroyed when switching scene.

    using UnityEngine;
    public class DontDestroyOnLoad : MonoBehabiour {
    void Awake()
    {
    DontDestroyOnLoad(gameObject);
    }
    }
     
  22. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    Thanks for your hint with the "DontDestroyOnLoad" script. I just tried your solution, but sadly that didn't fix the problem. I then also placed the script on the AR-camera (where the background and manager scripts are placed on), but that also didn't fix it and I'm getting still the black screen after loading a AR-scene more than once :(
     
  23. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    Ok, the black screen after scene-change is more easier to fix, than I thought! I created a small example project to commit a bugreport and while creating that project I discovered that I was just about to install the preview package of ARFoundation, again. As a software-developer one should be aware that preview versions might not be that 100% tested and debugged... so I went with the latest version without "preview" and the problem was solved. So just install the packages that are NOT in preview and the black screen is gone. Only fools like me install preview packages and wonder, why they aren't perfectly working :rolleyes: So sorry for all confusions from my side to this topic.

    The project now works with the followig packages:
    • ARFoundation 2.1.3
    • ARCore 2.1.1
    • ARKit 2.1.1
    I hope this might help somebody else :)
     
    NitinAltran likes this.
  24. LuisCarlosContreras

    LuisCarlosContreras

    Joined:
    Aug 7, 2018
    Posts:
    3
    Welp, I did set those packages and the problem persist... still black camera, I see is a common issue on forums
     
  25. ziemlich3D

    ziemlich3D

    Joined:
    Aug 21, 2017
    Posts:
    24
    Hmm, for my project this helps to fix the black screen problem after scene-change and didn't introduce it at reset. Also my sample-project does the Reset() and scene-change without the black screen multiple times.
    You are right, many people seem to report this issue, but the progression of ARFoundation is quite quick and maybe also the problems of these people are already fixed.
    Do you do some extra tasks in your reset-method that might cause problems? Are there any errors in the debug-output in the logcat?
     
  26. THDevelopment

    THDevelopment

    Joined:
    Jun 17, 2014
    Posts:
    2
    Hello there!

    I have a problem, when resetting the ARSession. I get a "rainbow disorder" which maybe has something to do with the camera texture, because you cant see it, if there is some GUI above.

    Do you guys may know how to avoid this? I attached one image, so you can get a better idea about the problem.

    It always appears when calling arSession.Reset(), or sometimes when reopen the Application and you´re in the active Session Screen.

    Every suggestion would be Great - Thank you very much!
     

    Attached Files:

    LangTao92 likes this.
  27. joepeijkemans

    joepeijkemans

    Joined:
    Apr 24, 2018
    Posts:
    6
    Are there any fixes yet? I have the same bug. It's not a big problem as everything works fine again after a second or so, but I have a loading screen in my app as well, so I might as well place that one there instead if possible.
     
  28. DeloitteCam

    DeloitteCam

    Joined:
    Jan 23, 2018
    Posts:
    22
  29. AAK_Lebanon

    AAK_Lebanon

    Joined:
    May 30, 2015
    Posts:
    74
    You can still hide it in a derived class and do whatever you want to do. note that they marked it as public, so you can call it from your script...
     
  30. DeloitteCam

    DeloitteCam

    Joined:
    Jan 23, 2018
    Posts:
    22
    Which is almost never a good idea.

    Reset() is documented to provide editor only functionality at the MonoBehaviour level then that functionality is hidden and reimplemented to provide entirely different runtime functionality at the ARSession level, that's straight up poor API design and I hope unity change it in the future.

    Just because you can isn't a good enough reason to justify the confusion this raises.
     
  31. LangTao92

    LangTao92

    Joined:
    May 13, 2017
    Posts:
    15

    How to solve this problem after reset arsession
     
    labpat likes this.
  32. labpat

    labpat

    Joined:
    Jan 24, 2020
    Posts:
    2
    How can we reset a MARS session to freshly generate/scan a new ground plane without the need of shutting down the app and restarting it?
     
  33. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    759
    As others pointed out before, one should call the arSession.Reset() to reset the AR session.

    But. Some AR subsystems don't reset properly after calling arSession.Reset(). Here are some example of such subsystems:
    • Feature points detected by ARPointCloudManager will not reset.
    • ARKit World Map will not clear the applied world map.
    • ARMeshManager will not reset detected meshes.
    • ARCameraManager.currentConfiguration will not be reset to the initial value.
    • ...probably, some other places I don't know of.

    Solution:
    To reset AR session completely, I use this method (official AR Foundation Samples repo uses it too):
    Code (CSharp):
    1.  
    2. var xrManagerSettings = UnityEngine.XR.Management.XRGeneralSettings.Instance.Manager;
    3. xrManagerSettings.DeinitializeLoader();
    4. UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex); // reload current scene
    5. xrManagerSettings.InitializeLoaderSync();
    6.  
     
    Last edited: Jul 17, 2021
    AkashInpro, LPLaser, chealin and 6 others like this.
  34. nir11

    nir11

    Joined:
    Oct 13, 2017
    Posts:
    8
    Thank you, your code helped me! I do not get a black screen, so sorry I can't help you there.
     
  35. Sound-Master

    Sound-Master

    Joined:
    Aug 1, 2017
    Posts:
    40
    You are an absolute life saver!

    Could you point me in the direction where to find this code in the official ARfoundation samples repo? I only found the part where they use
    Code (CSharp):
    1. LoaderUtility.Initialise()
    and
    Code (CSharp):
    1. LoaderUtility.Deinitialise()
    but I found that it doesn't always work, especially when going from AR scene to AR scene. It seems to work when going from NON AR Scene to AR Scene and back, which is what they do in the repo.
     
  36. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    759
    LoaderUtility.Initialise/Deinitialise() is just a handy wrapper for XRGeneralSettings.Instance.Manager.DeinitializeLoader/InitializeLoaderSync(). The only reason why I use the latter is that it works both in AR Foundation 3 and 4.
     
  37. Sound-Master

    Sound-Master

    Joined:
    Aug 1, 2017
    Posts:
    40
    Thank you! This makes sense, I thought it could be the case.

    However, I noticed that the ARFoundation samples repo uses this sequnce to load an AR scene from a non AR scene:

    Code (CSharp):
    1. LoaderUtility.Initialize();
    2. SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
    while when going from an AR scene to a non AR scene (the main menu) they use this sequence:

    Code (CSharp):
    1.             SceneManager.LoadScene("Menu", LoadSceneMode.Single);
    2. LoaderUtility.Deinitialize();
    which kind of makes sense.

    However, do you think to go from an AR scene to anothe AR scene I should do the following?:

    Code (CSharp):
    1. LoaderUtility.Initialize();
    2. SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
    3. LoaderUtility.Deinitialize();
    Also do you know why the Deinitialize() function is called after the scene load call?

    When I do this however, I receive this error in the Logcat (Android). See atached image.

    Many Thanks and sorry for all the questions!
     

    Attached Files:

    Last edited: Jan 18, 2021
  38. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    369
  39. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,699
    saifshk17 likes this.
  40. bibloc

    bibloc

    Joined:
    Jan 31, 2018
    Posts:
    17
    We try to clear AR session without changing scene with this code :

    Code (CSharp):
    1. public void ResetSession() {
    2.    LoaderUtility.Deinitialize ();
    3.    LoaderUtility.Initialize ();
    4. }
    But camera video does not restart.
    We use Unity 2020.3.5f1 with ARFoundation 4.1.7.

    Why doesn't it work ?
     
    Last edited: Apr 28, 2021
  41. ARGO_phil

    ARGO_phil

    Joined:
    Jun 15, 2021
    Posts:
    1
    Hi everyone!
    I am currently working on a little demo app that includes 2 scenes that work with the image tracking manager of AR foundation. If I switch between those scenes in one session with an intermediate switch to a scene-picking menu, they don't behave correctly - for instance, the 2nd scene loads the prefab instantly when the reference image for scene 1 is still open.
    So I figured calling
    LoaderUtility.Deinitialize()
    on the "return" button and the initialize-equivalent in the main menu would do the job, just like it does in the foundation samples and as described here.

    However, my application freezes whenever I switch back to the main menu, when the
    LoaderUtility.Deinitialize()
    -method is called and I get the following error log in my Android LogCat:

    [XRInputSubsystem] A device disconnection with the id 0 has been reported but no device with that id was connected.

    I am working with Unity version 2020.3.12f1 and AR Foundation version 4.1.7
    For my UI I work with the preview.14 version of UI builder and UI toolkit. Using the unity build-in ui-system for the back button instead didn't stop the freezing problem.

    Any idea anyone what I can do about this or someone with the same issue when calling that method?
    Cheers!
     
    Last edited: Sep 10, 2021
    tonialatalo likes this.
  42. jordan_subvrsive

    jordan_subvrsive

    Joined:
    Aug 2, 2021
    Posts:
    2
    Hi all, it seems that after initializing/deinitializing the AR foundation subsystems multiple times in a row, there's a memory leak that causes my application to crash. Has anyone else experienced memory/performance issues when using LoaderUtility.Initlialize/Deinitialize and/or xrManagerSettings.Initialize/DeinitializeLoader()?
     
unityunity