Unity-初始放置后禁用AR HitTest

时间:2018-06-24 01:54:24

标签: ios unity3d augmented-reality arkit hittest

我正在使用ARKit插件通过UnityARHitTestExample.cs来使用Unity。

将对象放置到世界场景中后,我想禁止ARKit在每次触摸屏幕时尝试再次放置该对象。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

您可以通过多种方法来实现,尽管最简单的方法是创建一个boolean来确定是否放置了模型。

首先,您将如上所述创建一个boolean,例如:

private bool modelPlaced = false;

然后,一旦放置了模型,您就可以在true函数中将其设置为HitTestResultType

bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes)
{
    List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes);
    if (hitResults.Count > 0) {
        foreach (var hitResult in hitResults) {

            //1. If Our Model Hasnt Been Placed Then Set Its Transform From The HitTest WorldTransform
            if (!modelPlaced){

                m_HitTransform.position = UnityARMatrixOps.GetPosition (hitResult.worldTransform);
                m_HitTransform.rotation = UnityARMatrixOps.GetRotation (hitResult.worldTransform);
                Debug.Log (string.Format ("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));

                //2. Prevent Our Model From Being Positioned Again
                modelPlaced = true;
            }


            return true;
        }
    }
    return false;
}

然后在Update()函数中:

void Update () {

    //Only Run The HitTest If We Havent Placed Our Model
    if (!modelPlaced){

        if (Input.touchCount > 0 && m_HitTransform != null)
        {
            var touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
            {
                var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point = new ARPoint {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                ARHitTestResultType[] resultTypes = {
                    ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, 
                }; 

                foreach (ARHitTestResultType resultType in resultTypes)
                {
                    if (HitTestWithResultType (point, resultType))
                    {
                        return;
                    }
                }
            }
        }
    }   
}

希望有帮助...