如何使用Unity停止平面检测并删除ARCore 1.9中的所有平面?

时间:2019-06-17 07:36:47

标签: c# android unity3d arcore

我要在屏幕上显示一个模型。检测到平面时,我点按设备屏幕以显示该模型。当场景中显示的平面笨拙并再次检测平面时会出现问题。 ARkit有任何事件可以从场景中删除锚点。当我通过触摸将模型放置在检测到的平面上时,我正在呼吁关闭平面检测并删除平面的方法。它不起作用。下面给出代码。

//Method call to turn of plane detection n delete already detected planes
 DetectedPlaneGenerator.DPGenaratorInstance.DeletePlanes();

检测到的平面生成器类,我在其中编写了一些方法来关闭平面检测并删除已经找到的平面。

 public class DetectedPlaneGenerator : MonoBehaviour
{
    /// <summary>
    /// A prefab for tracking and visualizing detected planes.
    /// </summary>
    public GameObject DetectedPlanePrefab;
    GameObject planeObject;
    public Text Msgtxt;

    /// <summary>
    /// A list to hold new planes ARCore began tracking in the current frame. This object is
    /// used across the application to avoid per-frame allocations.
    /// </summary>
    private List<DetectedPlane> m_NewPlanes = new List<DetectedPlane>();

    /// <summary>
    /// The Unity Update method.
    /// </summary>
    /// 

    public static DetectedPlaneGenerator DPGenaratorInstance;



    private void Start()
    {
        DPGenaratorInstance = this;
    }
    public void Update()
    {
        // Check that motion tracking is tracking.
        if (Session.Status != SessionStatus.Tracking)
        {
            return;
        }

        // Iterate over planes found in this frame and instantiate corresponding GameObjects to
        // visualize them.
        Session.GetTrackables<DetectedPlane>(m_NewPlanes, TrackableQueryFilter.New);
        for (int i = 0; i < m_NewPlanes.Count; i++)
        {
            // Instantiate a plane visualization prefab and set it to track the new plane. The
            // transform is set to the origin with an identity rotation since the mesh for our
            // prefab is updated in Unity World coordinates.
             planeObject =
                Instantiate(DetectedPlanePrefab, Vector3.zero, Quaternion.identity, transform);
            planeObject.GetComponent<DetectedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
        }
    }


    public void DeletePlanes()
    {
        StopTracking();
        for (int i = 0; i < m_NewPlanes.Count; i++)
        {
            Destroy(planeObject);
        }

    }


    public void StopTracking()
    {
        int i = 0;
        //Msgtxt.text = i.ToString();

        foreach (GameObject plane in GameObject.FindGameObjectsWithTag("Plane"))
        {
            Renderer r = plane.GetComponent<Renderer>();
            DetectedPlaneVisualizer t = plane.GetComponent<DetectedPlaneVisualizer>();
            r.enabled = false;
            t.enabled = false;
           // Msgtxt.text = i.ToString();
            i++;//to check whether it is looping-no luck
        }


    }

}

1 个答案:

答案 0 :(得分:0)

您可以重置ARSession以删除所有平面

public ARSession session; 
public void ResetArSession(Scene scene)
{
    session.Reset();
    EntityManager.instance.DestroyAllEntity();
}