如何使用Unity中ARkit助手类的事件删除检测到的飞机锚点?

时间:2019-05-28 10:09:07

标签: c# ios unity3d arkit

我正在UnityARHitSceneExample中测试一个场景。放置模型时,我试图删除所有检测到的平面,并且关闭平面检测也会删除锚点,因为放置模型后,锚点仍然显示很尴尬。有时它总是很容易混淆,有时却没有。因此,现在我尝试使用ARkit帮助程序类中提供的事件的其他方法。我在UnityARAnchorManager类中找到了以下代码。因此,如何使用此代码删除所有检测到的锚点。

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;
    }
}

下面的代码是我要删除锚点的一种方法。

 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();
        }


}

我还在UnityARCameraManager内部使用了一种方法来停止平面检测,如下所示。

 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);
    }

}

通过使用StopPlaneDetection()我们可以停止进一步的检测。在这里,主要的问题是删除检测到的锚点,该锚点是ARKit在检测平面时放置的平面网格。 当我尝试此代码时,将Xcode中的异常作为InvalidOperationException:列表已修改。我已经看过几次这个问题,但是还没有解决这个问题的可靠方法。我也在IOS中看到了这一点。

0 个答案:

没有答案