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



我想点击屏幕并显示模型。当检测到飞机时,我点击设备屏幕显示模型。当场景中显示的平面很尴尬并且它再次检测飞机时,就会出现问题。与 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
        }

    }
}

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

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

最新更新