Search Unity

Remove Anchors from real world using ARkit?

Discussion in 'Scripting' started by zyonneo, May 28, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Hi I testing out a scene in UnityARHitSceneExample.I am trying to remove all detected planes when I place a model and turn off plane detection.It is always confusing sometimes it works sometimes it will not.So Now I am trying different using Events provided in the ARkit helper class.I found the below code in UnityARAnchorManager class.So how could I use this code to delete all detected anchors.
    Code (CSharp):
    1.  
    2. //Main methods from  UnityARAnchorManager Class.
    3. public LinkedListDictionary<string, ARPlaneAnchorGameObject> planeAnchorMap;
    4.  
    5. //Events are
    6. public UnityARAnchorManager ()
    7.         {
    8.             planeAnchorMap = new LinkedListDictionary<string,ARPlaneAnchorGameObject> ();
    9.             UnityARSessionNativeInterface.ARAnchorAddedEvent += AddAnchor;
    10.             UnityARSessionNativeInterface.ARAnchorUpdatedEvent += UpdateAnchor;
    11.             UnityARSessionNativeInterface.ARAnchorRemovedEvent += RemoveAnchor;
    12.  
    13.         }
    14.  
    15.  
    16. public void AddAnchor(ARPlaneAnchor arPlaneAnchor)
    17.         {
    18.             GameObject go = UnityARUtility.CreatePlaneInScene (arPlaneAnchor);
    19.             go.AddComponent<DontDestroyOnLoad> ();  //this is so these GOs persist across scene loads
    20.             ARPlaneAnchorGameObject arpag = new ARPlaneAnchorGameObject ();
    21.             arpag.planeAnchor = arPlaneAnchor;
    22.             arpag.gameObject = go;
    23.             planeAnchorMap.Add (arPlaneAnchor.identifier, arpag);
    24.         }
    25.  
    26.  
    27.  
    28. public void RemoveAnchor(ARPlaneAnchor arPlaneAnchor)
    29.         {
    30.             if (planeAnchorMap.ContainsKey (arPlaneAnchor.identifier)) {
    31.                 ARPlaneAnchorGameObject arpag = planeAnchorMap [arPlaneAnchor.identifier];
    32.                 GameObject.Destroy (arpag.gameObject);
    33.                 planeAnchorMap.Remove (arPlaneAnchor.identifier);
    34.             }
    35.         }
    36.  
    The planeAnchorMap contains all the detected anchors but how to delete items from the Linkedlist dictionary remains an issue.

    I called the RemoveAnchore method at the time when I wanted to delete all detected planes but did not work out .Code Given below
    Code (CSharp):
    1.       public void DeleteRealWorldPlanes()
    2.       {
    3.  
    4.             Debug.Log("Entered DeleteRealWorldPlanes");
    5. //Called RemoveAnchor using an object from UnityARAnchorManager Class.
    6.           UnityARSessionNativeInterface.ARAnchorRemovedEvent += unityARAnchorManager.RemoveAnchor;
    7.    }
     
  2. SGM3

    SGM3

    Joined:
    Jun 10, 2013
    Posts:
    81
    I am unfamiliar with this Unity example. However, it does not appear that you understand delegates. Check out this video:


    This should give you a better insight to how events and delegates work. However, do note that the video maintains .net standards, and they are not necessarily the best way to work with unity, but the functionality is the same.
     
    Last edited: May 30, 2019
    zyonneo likes this.
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Somehow managed to delete the real world Anchors.... thanks...:)