I was testing out a scene in UnityARHitSceneExample. I am trying to remove all detected planes when I place a model and turn off plane detection also remove the anchors, as it is awkward after placing the model the anchor is still showing. It is always confusing sometimes it works, sometimes it will not. So Now I am trying different method 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.

public class UnityARAnchorManager 
{


    public LinkedListDictionary<string, ARPlaneAnchorGameObject> planeAnchorMap;


    public UnityARAnchorManager ()
    {
        planeAnchorMap = new LinkedListDictionary<string,ARPlaneAnchorGameObject> ();
        UnityARSessionNativeInterface.ARAnchorAddedEvent += AddAnchor;
        UnityARSessionNativeInterface.ARAnchorUpdatedEvent += UpdateAnchor;
        UnityARSessionNativeInterface.ARAnchorRemovedEvent += RemoveAnchor;

    }


    public void AddAnchor(ARPlaneAnchor arPlaneAnchor)
    {
        GameObject go = UnityARUtility.CreatePlaneInScene (arPlaneAnchor);
        go.AddComponent<DontDestroyOnLoad> ();  //this is so these GOs persist across scene loads
        ARPlaneAnchorGameObject arpag = new ARPlaneAnchorGameObject ();
        arpag.planeAnchor = arPlaneAnchor;
        arpag.gameObject = go;
        planeAnchorMap.Add (arPlaneAnchor.identifier, arpag);
    }

    public void RemoveAnchor(ARPlaneAnchor arPlaneAnchor)
    {

        Debug.Log("Entered RemoveAnchor");
        if (planeAnchorMap.ContainsKey (arPlaneAnchor.identifier)) {
            ARPlaneAnchorGameObject arpag = planeAnchorMap [arPlaneAnchor.identifier];
            GameObject.Destroy (arpag.gameObject);
            planeAnchorMap.Remove (arPlaneAnchor.identifier);
        }
    }

    public void UpdateAnchor(ARPlaneAnchor arPlaneAnchor)
    {
        if (planeAnchorMap.ContainsKey (arPlaneAnchor.identifier)) {
            ARPlaneAnchorGameObject arpag = planeAnchorMap [arPlaneAnchor.identifier];
            UnityARUtility.UpdatePlaneWithAnchorTransform (arpag.gameObject, arPlaneAnchor);
            arpag.planeAnchor = arPlaneAnchor;
            planeAnchorMap [arPlaneAnchor.identifier] = arpag;
        }
    }

    public void UnsubscribeEvents()
    {
        UnityARSessionNativeInterface.ARAnchorAddedEvent -= AddAnchor;
        UnityARSessionNativeInterface.ARAnchorUpdatedEvent -= UpdateAnchor;
        UnityARSessionNativeInterface.ARAnchorRemovedEvent -= RemoveAnchor;
    }

    public void Destroy()
    {
        foreach (ARPlaneAnchorGameObject arpag in GetCurrentPlaneAnchors()) {
            GameObject.Destroy (arpag.gameObject);
        }

        planeAnchorMap.Clear ();
        UnsubscribeEvents();
    }

    public LinkedList<ARPlaneAnchorGameObject> GetCurrentPlaneAnchors()
    {
        return planeAnchorMap.Values;
    }
}

Below code is a method when I want to remove the anchors.

 public void RestartPlaneGenerator()
   {


        Debug.Log("Entered Restart Genrator");
        UnityARCameraManager.InstanceCamManger.StopPlaneDetection();//Method given below


        //DestroyAllPlanes();
        //unityARAnchorManager.Destroy();
        Debug.Log("B4 delete " );
        foreach (ARPlaneAnchorGameObject arpag in unityARAnchorManager.GetCurrentPlaneAnchors())
        {
            Debug.Log("Entered Delete "+arpag.gameObject + " : "+arpag.planeAnchor);
       //Here getting names for object an planeAnchor but cannot delete it
            GameObject.Destroy(arpag.gameObject);
            unityARAnchorManager.planeAnchorMap.Clear();
           // unityARAnchorManager.planeAnchorMap.Remove();
        }


}

I also used a method inside UnityARCameraManager to stop plane detection, given below.

 public void StopPlaneDetection()
{
    Debug.Log("Worked");
    m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface();

    config.planeDetection = UnityARPlaneDetection.None;
    config.alignment = UnityARAlignment.UnityARAlignmentGravity;
    config.getPointCloudData = false;
    config.enableLightEstimation = enableLightEstimation;

    UnityARSessionRunOption runOption = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors;

    if (config.IsSupported)
    {
        m_session.RunWithConfigAndOptions(config,runOption);
    }

}

By using StopPlaneDetection() we can stop further detection.Removing the detected anchors that is the plane mesh placed by ARKit on detection of a plane is the main issue here. When I tried this code getting the exception in Xcode as InvalidOperationException: list modified. I have seen this question couple of times but not a solid fix for this issue. I have seen this in IOS too.

0 Answers