【问题标题】:ARCore: execute each frame instead of when touching the screen (Unity)ARCore:执行每一帧而不是在触摸屏幕时执行(Unity)
【发布时间】:2018-04-13 11:47:16
【问题描述】:

我正在一个项目中工作,该项目需要每帧执行一个功能,但无需单击屏幕。我不知道为什么将代码放在 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之类的......

非常感谢您!

【问题讨论】:

  • 如果它直接在 Update() 中,我看不出它不会执行的原因。请发布更多相关代码以显示它们的调用位置。

标签: unity3d augmented-reality arcore


【解决方案1】:

好的,这里是完整的更新功能。我的部分代码在最后几行,在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();
    }

【讨论】:

    【解决方案2】:

    好的,伙计们,我明白了。您只需将要应用的代码放在每一帧中,就在触摸屏幕时放置 Android 预制件的“if”上方。 我不知道为什么,但这有效。希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-30
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      相关资源
      最近更新 更多