ARCore:执行每个帧而不是触摸屏幕(Unity)

时间:2018-04-13 11:47:17

标签: unity3d augmented-reality arcore

我正在一个需要每帧执行一个功能但不点击屏幕的项目中工作。我不知道为什么把代码放在Update函数中不起作用(它需要所有时间,无论如何,触摸屏幕来执行代码)。我在这里放了我想要每帧播放的代码部分:

     anchor.transform.position = Vector3.zero; 
     hotPoint.transform.position = Vector3.zero;
     _androidPosVector3ZeroText.text = anchor.transform.position.ToString(); 
     _camPosText.text = FirstPersonCamera.transform.position.ToString();

我将解释它是如何工作的:我已将android图标及其锚点放在0,0,0世界位置。我这样做是因为我总是想知道应用程序中我的世界中心在哪里。最后,我想知道相对于该中心的相机位置的每一帧。我在文本中显示每个值。这段代码在Update函数中,我不知道为什么我必须触摸屏幕执行它,如果它不在任何if或者什么东西......

提前非常感谢你!

2 个答案:

答案 0 :(得分:0)

好的,这是完整的Update功能。我的部分代码在最后一行,并在Update中直接调用。

    public void Update()

    {
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }

        _QuitOnConnectionErrors();

        // Check that motion tracking is tracking.
        if (Session.Status != SessionStatus.Tracking)
        {
            const int lostTrackingSleepTimeout = 15;
            Screen.sleepTimeout = lostTrackingSleepTimeout;
            if (!m_IsQuitting && Session.Status.IsValid())
            {
                SearchingForPlaneUI.SetActive(true);
            }

            return;
        }

        Screen.sleepTimeout = SleepTimeout.NeverSleep;

        // Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
        Session.GetTrackables<TrackedPlane>(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.
            GameObject planeObject = Instantiate(TrackedPlanePrefab, Vector3.zero, Quaternion.identity,
                transform);
            planeObject.GetComponent<TrackedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
        }

        // Disable the snackbar UI when no planes are valid.
        Session.GetTrackables<TrackedPlane>(m_AllPlanes);
        bool showSearchingUI = true;
        for (int i = 0; i < m_AllPlanes.Count; i++)
        {
            if (m_AllPlanes[i].TrackingState == TrackingState.Tracking)
            {
                showSearchingUI = false;
                break;
            }
        }

        SearchingForPlaneUI.SetActive(showSearchingUI);

        // If the player has not touched the screen, we are done with this update.
        Touch touch;
        if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
        {
            return;
        }

        // Raycast against the location the player touched to search for planes.
        TrackableHit hit;
        TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
            TrackableHitFlags.FeaturePointWithSurfaceNormal;


        Frame.Raycast(0.5f, 0.5f, raycastFilter, out hit);
        float distance = hit.Distance;

        if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit) && Time.time < 15)
        {
            hotPoint = Instantiate(AndyAndroidPrefab, Vector3.zero, hit.Pose.rotation); 

            // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
            // world evolves.
            anchor = hit.Trackable.CreateAnchor(hit.Pose);

            anchor.transform.position = hotPoint.transform.position; 
            _androidPosVector3ZeroText.text = anchor.transform.position.ToString(); 
            _camPosText.text = FirstPersonCamera.transform.position.ToString(); 

            // Andy should look at the camera but still be flush with the plane.
            if ((hit.Flags & TrackableHitFlags.PlaneWithinPolygon) != TrackableHitFlags.None)
            {
                // Get the camera position and match the y-component with the hit position.
                Vector3 cameraPositionSameY = FirstPersonCamera.transform.position;
                cameraPositionSameY.y = hit.Pose.position.y;

                // Have Andy look toward the camera respecting his "up" perspective, which may be from ceiling.
                hotPoint.transform.LookAt(cameraPositionSameY, hotPoint.transform.up);
            }

            // Make Andy model a child of the anchor.
            //hotPoint.transform.parent = anchor.transform;

            hotPoints.Add(hotPoint); 

            hotPointDistanceToCamera = Vector3.Distance(FirstPersonCamera.transform.position, anchor.transform.position); 
            hotPointDistancesToCamera.Add(hotPointDistanceToCamera); 
            distanceTexts[0].text = "Hot Point " + (Time.time.ToString()) + ": " + hotPointDistancesToCamera[0].ToString();


            Text newText = Instantiate(distanceText, canvas.transform, false); 
            distanceTexts.Add(newText); 
            if (lastText != null)
            {
                newText.rectTransform.localPosition = lastText.rectTransform.localPosition + new Vector3(0, -30, 0); 
            }
            lastText = newText;
        }

        anchor.transform.position = Vector3.zero; 
        hotPoint.transform.position = Vector3.zero;

        _androidPosVector3ZeroText.text = anchor.transform.position.ToString(); 

        _camPosText.text = FirstPersonCamera.transform.position.ToString();
    }

答案 1 :(得分:0)

好的,伙计们,我明白了。你只需要在每一帧中放置你想要的代码,就在&#34; if&#34;这使得在触摸屏幕时放置Android预制件。 我不知道为什么,但这有效。希望能有所帮助。