【问题标题】:Unity - Disable AR HitTest after initial placementUnity - 在初始放置后禁用 AR HitTest
【发布时间】:2018-06-24 01:54:24
【问题描述】:

我正在利用 UnityARHitTestExample.cs 为 Unity 使用 ARKit 插件。

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

【问题讨论】:

    标签: ios unity3d augmented-reality arkit hittest


    【解决方案1】:

    您可以通过多种方式实现此目的,但最简单的方法可能是创建boolean 来确定您的模型是否已放置。

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

    private bool modelPlaced = false;
    

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

    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;
                        }
                    }
                }
            }
        }   
    }
    

    希望对你有帮助...

    【讨论】:

    • 很高兴它有帮助:) 请标记为正确答案,这样可以显示为完整:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多