首页 > 解决方案 > AR Foundation - 在屏幕上放置 2 次触控的预制件

问题描述

我正在为建筑行业开发增强现实应用程序,需要以 1:1 的比例放置模型。我知道如何一键放置,但我想知道是否可以设置 2 个点来锚定我的模型。

提前致谢!!!

标签: c#unity3daugmented-reality

解决方案


您可以使用if (Input.touchCount == 1)(同时)检测不止一个手指轻敲。然后,您可以根据两指触摸放置对象。

            // Store both touches.
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne = Input.GetTouch(1);
            // Calculate previous position
            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
            // Find the magnitude of the vector (the distance) between the touches in each frame.
            float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
            // Find the difference in the distances between each frame.
            float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
            float pinchAmount = deltaMagnitudeDiff * 0.02f * Time.deltaTime;
            spawnedObject.transform.localScale -= new Vector3(pinchAmount, pinchAmount, pinchAmount);
            // Clamp scale
            float Min = 0.005f;
            float Max = 3f;
            spawnedObject.transform.localScale = new Vector3(
                Mathf.Clamp(spawnedObject.transform.localScale.x, Min, Max),
                Mathf.Clamp(spawnedObject.transform.localScale.y, Min, Max),
                Mathf.Clamp(spawnedObject.transform.localScale.z, Min, Max)
            );

推荐阅读