ARCore Unity:如何根据命令启动和停止平面检测



我正在使用 ARCore 创建一个应用程序,但我不希望 ARCore 在应用程序启动后立即寻找飞机。相反,我希望在点击应用中的按钮时开始平面检测。如果我也能根据命令停止飞机检测,那就太好了。

有谁知道如何根据命令启动和停止 ARCore 平面检测?

我正在 Unity 中构建应用程序。

提前非常感谢!

ARPlaneVisualizer.cs上,有这段代码

    void OnEnable()
    {
        m_PlaneLayer = LayerMask.NameToLayer ("ARGameObject");
        ARInterface.planeAdded += PlaneAddedHandler;
        ARInterface.planeUpdated += PlaneUpdatedHandler;
        ARInterface.planeRemoved += PlaneRemovedHandler;
        HidePlane(true);
    }
    void OnDisable()
    {
        ARInterface.planeAdded -= PlaneAddedHandler;
        ARInterface.planeUpdated -= PlaneUpdatedHandler;
        ARInterface.planeRemoved -= PlaneRemovedHandler;
        HidePlane(false);
    }

您可以使用OnEnable()代码作为开始跟踪,OnDisable()代码停止跟踪。

最初创建 bool 以限制表面检测代码,并在不知不觉中使 bool 变为 true。

bool isSurfaceDetected = true;
if (isSurfaceDetected) {

            Session.GetTrackables<TrackedPlane> (_newPlanes, TrackableQueryFilter.New);
            // Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
            foreach (var curPlane in _newPlanes) {
                // 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.
                var planeObject = Instantiate (plane, Vector3.zero, Quaternion.identity,
                                      transform);
                planeObject.GetComponent<DetectedPlaneVisualizer> ().Initialize (curPlane);
                //              Debug.Log ("test....");
                // Apply a random color and grid rotation.
                //          planeObject.GetComponent<Renderer>().material.SetColor("_GridColor", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f)));
                //          planeObject.GetComponent<Renderer>().material.SetFloat("_UvRotation", Random.Range(0.0f, 360.0f));
                //
            }

在画布中创建一个停止按钮并附加下面的方法

public void StopTrack()
    {
        // Make isSurfaceDetected to false to disable plane detection code
        isSurfaceDetected = false;
        // Tag DetectedPlaneVisualizer prefab to Plane(or anything else)
        GameObject[] anyName = GameObject.FindGameObjectsWithTag ("Plane");
        // In DetectedPlaneVisualizer we have multiple polygons so we need to loop and diable DetectedPlaneVisualizer script attatched to that prefab.
        for (int i = 0; i < anyName.Length; i++) 
        {
            anyName[i].GetComponent<DetectedPlaneVisualizer> ().enabled = false;
        }
    }

确保停止按钮方法在ARController中

最新更新