Why Update Function not working in UnityARHitTestExample of UnityARWorldMap Scene in Unity?

孤街浪徒 提交于 2019-12-23 22:13:04

问题


I am working on UnityARWorldMap to create persistance in unity.So I place the models in real world, save them and later load them.When I click Load() in WorldMapManager.cs the update function in UnityARhitTestExample stops updating.The WorldMapManager script is given below.

public void Save()
{

//Saving the data into Application.persistentDataPath

}



public void Load()
{

//Loads the data from Application.persistentDataPath

Debug.LogFormat("Loading ARWorldMap {0}", path);
        var worldMap = ARWorldMap.Load(path);
        if (worldMap != null)
        {
            m_LoadedMap = worldMap;
            Debug.LogFormat("Map loaded. Center: {0} Extent: {1}", worldMap.center, worldMap.extent);

            UnityARSessionNativeInterface.ARSessionShouldAttemptRelocalization = true;

            var config = m_ARCameraManager.sessionConfiguration;
            config.worldMap = worldMap;
            UnityARSessionRunOption runOption = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking;

            Debug.Log("Restarting session with worldMap");
            session.RunWithConfigAndOptions(config, runOption);

       }

}

In the Update function UnityARHitTestExample.cs I have given a statement to Print.It stops printing when I hit Load button.Why is that?

    public class UnityARHitTestExample : MonoBehaviour
{

    public static UnityARHitTestExample HittestInstance;
    public GameObject[] Allmodels;
    public Transform m_HitTransform;
    public float maxRayDistance = 30.0f;
    public LayerMask collisionLayer = 1 << 10;  //ARKitPlane layer

    bool placed = false;
    public List<DataHandlers> Modelinfo = new List<DataHandlers>();
     List<GameObject> ContainerObject = new List<GameObject>();

    GameObject currentModel;
    string json;
    int indexchoice;
    GameObject temp;
    List<int> indexarray = new List<int>() ;


    float rotateValue = 300f;

    //Original Camera Parent position
    public Transform CameraParent;
    private Vector3 Campos;
    public Text Positions;




    bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes)
    {
        List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes);

        if (hitResults.Count > 0 ) {
            foreach (var hitResult in hitResults) {
                if (currentModel != null)
                {
                    Debug.Log("Got hit!");
                   temp = Instantiate(currentModel);
                    ContainerObject.Add(temp);
                    placed = true;

                   temp.transform.position=UnityARMatrixOps.GetPosition(hitResult.worldTransform);
                    temp.transform.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;
    }


    private void Start()
    {
        HittestInstance = this;
    }


    private void LateUpdate()
    {


        Camera.main.transform.SetParent(CameraParent.transform);
    }

    private void Update()

    {
        Debug.Log("Changed ");



        #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 == 1 && m_HitTransform != null && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) )
        {
            var touch = Input.GetTouch(0);
            //if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
            if (touch.phase == TouchPhase.Began && isLocked==false )
            {
                var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
                ARPoint point = new ARPoint {
                    x = screenPosition.x,
                    y = screenPosition.y
                };

                //User Initial choice was for existing plane & Horizontal plane 
                // 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
    }

来源:https://stackoverflow.com/questions/55512273/why-update-function-not-working-in-unityarhittestexample-of-unityarworldmap-scen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!