Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to stop generating and or destroy all planes

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

  1. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    260
    Just wanted to drop this knowledge in a public place, because I lost many hours/days trying to figure it out. With the Unity ARKit plugin, there is no clear or obvious way to reset planes or stop planes from continuing to generate. Here's some basic knowledge about what's going on.

    There are two separate versions of the "planes" being generated as your user is scanning the world in an ARKit/Unity app. There are the "anchors" which seem to be stored on the native side itself, independent of a specific ARSession. Then there's the actual spawned "planes" which are instances of the prefab spawned into the scene by the UnityARGeneratePlane class. Every time a new "anchor" is found and created on the native side, this class spawns a "plane" (or mesh plane in ARKit2.0) into the scene.


    If you want to stop the planes from continuing to generate, while keeping the ones already found, you need to restart the ARSession with RunWithConfigAndOptions and make sure the config.planeDetection is set to none. By default the ARSession will KEEP all the previous "anchors" and tracking position of the camera in the scene. Your spawned prefab planes generated by UnityARGeneratePlane will also be kept by default because they are kept in the scene (the spawned "planes" also have a DontDestroyOnLoad component that makes them persist between scenes, remove the line where this component is added in code if you don't want that behavior!)


    Code (CSharp):
    1.    
    2. // restarts the ar session with plane detection turned off
    3. // but by default keeps already generated planes and anchors in the scene
    4. public void StopPlaneDetection()
    5. {
    6.     UnityARSessionNativeInterface session = UnityARSessionNativeInterface.GetARSessionNativeInterface();
    7.     ARKitWorldTrackingSessionConfiguration config = new ARKitWorldTrackingSessionConfiguration();
    8.     config.planeDetection = UnityARPlaneDetection.None;
    9.     session.RunWithConfig(config);
    10. }
    11.  

    By contrast, if you want to completely destroy all planes in the scene, you need to manually destroy all the spawned planes in the scene, and also reset the ARSession with RunWithConfigAndOptions. When you call this method you also need to pass in the options RemoveExistingAnchors and RunOptionResetTracking, otherwise there will be no spawned "planes" in the scene, but the "anchors" will still be there on the native side, and doing AR hit tests will still result in positives.

    Code (CSharp):
    1.  
    2. // restarts the ar session (you can turn plane detection on or off with config
    3. // but passes in runOptions that remove anchors and resets scene tracking
    4. public void ResetPlaneDetection()
    5. {
    6.     m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface();
    7.  
    8.     var config = sessionConfiguration;
    9.     if (config.IsSupported)
    10.     {
    11.         UnityARSessionRunOption runOptions = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking;
    12.         m_session.RunWithConfigAndOptions (config, runOptions);
    13.         UnityARSessionNativeInterface.ARFrameUpdatedEvent += FirstFrameUpdate;
    14.     }
    15. }
    16.  
    Code (CSharp):
    1.  
    2. // modifications done to the UnityARGeneratePlane class
    3. // to enable better destroying of plane instances
    4. void Start ()
    5. {
    6.     Init();
    7. }
    8.  
    9. void Init()
    10. {
    11.     unityARAnchorManager = new UnityARAnchorManager();
    12.     UnityARUtility.InitializePlanePrefab (planePrefab);
    13. }
    14.  
    15. public RestartPlaneGenerator()
    16. {
    17.             if (unityARAnchorManager != null)
    18.             {
    19.                 unityARAnchorManager.Destroy();          
    20.             }
    21.  
    22.             DestroyAllPlanes();
    23.  
    24.             Init();
    25. }
    26.  
    27. void DestroyAllPlanes()
    28. {
    29.     ARKitPlaneMeshRender[] planes = FindObjectsOfType<ARKitPlaneMeshRender>();
    30.  
    31.     Debug.Log("total planes before destroy: " + planes.Length);
    32.  
    33.     if (planes == null || planes.Length == 0)
    34.         return;
    35.  
    36.     for (int i = planes.Length - 1; i >= 0; i--)
    37.     {
    38.         Debug.Log("DESTROYING PLANE: " + i);
    39.         GameObject.Destroy(planes[i].gameObject);
    40.     }
    41. }
    42.  
     
  2. Ulises_P

    Ulises_P

    Joined:
    Feb 4, 2018
    Posts:
    15
    Hi, i just posted a question specifically about this issue, thanks for sharing the knowledge, It seems overly complex and counter-intuitive compared to ARCore.
     
  3. fraeri

    fraeri

    Joined:
    Nov 8, 2018
    Posts:
    64
    Hi,
    thank you very much for your thread! Would you mind to share the "sessionConfiguration" as well?
    Having trouble to make it right.

    And is there also a solution to remove the particles as well?
     
    EthanFischer likes this.
  4. Alex_F

    Alex_F

    Joined:
    Mar 4, 2014
    Posts:
    10
    Thank you very much for sharing!
    I was struggling with this too.
    I needed just a toggle to show-hide the planes. Actually ended up with setting debugPlaneMaterial alpha to 0 and back.
     
  5. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386

    Without restarting the tracking because of the relocalization how to hide the planes......https://stackoverflow.com/questions...-user-know-if-the-gameobjects-are-relocalized