如何使用Arkit和unity在UnityARHitTestExample场景中根据按钮单击显示不同的模型?

时间:2018-07-24 05:00:13

标签: c# unity3d arkit

嗨,我想更新我想在设备上显示或增强的内容。最初我选择一个按钮,它会显示一个模型。再次选择另一个按钮时,它必须带另一个模型但不更新。当我单击第二个按钮仅显示第一个模型。模型未更改。

using System;
using System.Collections.Generic;
using UnityEngine.EventSystems;

namespace UnityEngine.XR.iOS
{
public class UnityARHitTestExample : MonoBehaviour
{
    public Transform m_HitTransform;
    public float maxRayDistance = 30.0f;
    public LayerMask collisionLayer = 1 << 10;  //ARKitPlane layer

    public List<GameObject> Instaobj = new List<GameObject>();

    public Transform ForSelect;
    int Select;

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


                if (Select == 0)
                {

                    Instantiate(Instaobj[0], ForSelect);

                    Instaobj[1].SetActive(false);
                    Instaobj[2].SetActive(false);


                }


                if (Select == 1)
                {


                    Instantiate(Instaobj[1], ForSelect);
                    Instaobj[0].SetActive(false);
                    Instaobj[2].SetActive(false);

                }

                if (Select == 2)
                {

                    Instantiate(Instaobj[2], ForSelect);
                    Instaobj[0].SetActive(false);
                    Instaobj[1].SetActive(false);

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








                return true;
            }
        }
        return false;
    }

    // Update is called once per frame
    void Update () {


        #if UNITY_EDITOR   //we will only use this script on the editor side, though there is nothing that would prevent it from working on device
        if (Input.GetMouseButtonDown (0)) {
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit;

            //we'll try to hit one of the plane collider gameobjects that were generated by the plugin
            //effectively similar to calling HitTest with ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent
            if (Physics.Raycast (ray, out hit, maxRayDistance, collisionLayer)) {
                //we're going to get the position from the contact point
                m_HitTransform.position = hit.point;
                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));

                //and the rotation from the transform of the plane collider
                m_HitTransform.rotation = hit.transform.rotation;
            }
        }
        #else
        if (Input.touchCount > 0 && m_HitTransform != null )
        {
            var touch = Input.GetTouch(0);
            if ((touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) &&  !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
            {
                var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point = new ARPoint {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                // prioritize reults types
                ARHitTestResultType[] resultTypes = {
                    //ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingGeometry,
                    ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, 
                    // if you want to use infinite planes use this:
                    //ARHitTestResultType.ARHitTestResultTypeExistingPlane,
                    //ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane, 
                    //ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane, 
                    //ARHitTestResultType.ARHitTestResultTypeFeaturePoint
                }; 

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

    }




    public void SelectFunction(int choice)
    {

        Select = choice;


    } 




}


}

在SelectFuntion()方法中,我传递按钮单击值。当我单击第一个按钮时,它将传递0,第二个按钮将传递1,依此类推。仅对3个模型进行测试,但必须对许多模型进行测试。

我认为问题在于我使实例化对象为假。

0 个答案:

没有答案