// removes the object instance and detaches it from the world
    private void DestroyObjectInstance()
    {
        if (objectInstance)
        {
            // remove object anchor, if it was anchored before
            string anchorId = arManager.GetObjectAnchorId(objectInstance);
            if (anchorId != string.Empty)
            {
                arManager.RemoveGameObjectAnchor(anchorId, false);
            }

            // destroy object
            Destroy(objectInstance);
            objectInstance = null;
        }
    }
    // removes the model anchor
    private bool RemoveModelAnchor(Transform model)
    {
        // remove the anchor
        if (model && arManager)
        {
            // get the anchorId
            string anchorId = arManager.GetObjectAnchorId(model.gameObject);

            if (anchorId != string.Empty)
            {
                arManager.RemoveGameObjectAnchor(anchorId, false);
                return(true);
            }
        }

        return(false);
    }
예제 #3
0
    // invoked by the anchor-toggle
    public void AnchorToggleSelected(bool bOn)
    {
        if (bIntAction || !anchorTransform || !arManager)
        {
            return;
        }

        if (bOn)
        {
            // activate the anchor transform, if needed
            if (!anchorTransform.gameObject.activeSelf)
            {
                anchorTransform.gameObject.SetActive(true);
            }

            // create the anchor if needed
            if (!arManager.IsValidAnchorId(anchorId))
            {
                if (modelTransform && modelTransform.gameObject.activeSelf)
                {
                    // create the world anchor at model's position
                    anchorId = arManager.AnchorGameObjectToWorld(anchorTransform.gameObject, modelTransform.position, Quaternion.identity);
                }
                else
                {
                    // raycast center of the screen
                    //Vector2 screenPos = new Vector2(Screen.width / 2f, Screen.height / 2f);
                    MultiARInterop.TrackableHit hit;
                    if (arManager.RaycastToWorld(false, out hit))
                    {
                        // create the world anchor at hit's position
                        anchorId = arManager.AnchorGameObjectToWorld(anchorTransform.gameObject, hit);
                    }
                }

                // attach the model to the same anchor
                if (arManager.IsValidAnchorId(anchorId) && modelTransform && modelTransform.gameObject.activeSelf)
                {
                    arManager.AttachObjectToAnchor(modelTransform.gameObject, anchorId, false, false);
                }
            }
        }
        else
        {
            // remove the anchor as needed
            if (arManager.IsValidAnchorId(anchorId))
            {
                // create the world anchor
                if (arManager.RemoveGameObjectAnchor(anchorId, true))
                {
                    anchorId = string.Empty;
                }
            }

            // deactivate the anchor transform, if needed
            if (anchorTransform.gameObject.activeSelf)
            {
                anchorTransform.gameObject.SetActive(false);
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        // check for tap
        if (portalPrefab && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true))
        {
            MultiARInterop.InputAction action = arManager.GetInputAction();

            if (action == MultiARInterop.InputAction.Click)
            {
                // raycast to world
                MultiARInterop.TrackableHit hit;
                if (arManager.RaycastToWorld(true, out hit))
                {
                    // create the portal object, if needed
                    if (!portalObj)
                    {
                        portalObj = Instantiate(portalPrefab);
                    }

                    // set its position and rotation
                    portalObj.transform.position = hit.point;
                    portalObj.transform.rotation = !verticalPortal ? hit.rotation : Quaternion.identity;

                    // look at the camera
                    if (portalLookingAtCamera)
                    {
                        Camera arCamera = arManager.GetMainCamera();
                        MultiARInterop.TurnObjectToCamera(portalObj, arCamera, hit.point, hit.normal);
                    }

                    // remove object anchor, if it was anchored before
                    string anchorId = arManager.GetObjectAnchorId(portalObj);
                    if (anchorId != string.Empty)
                    {
                        arManager.RemoveGameObjectAnchor(anchorId, true);
                    }

                    // anchor it to the new world position
                    arManager.AnchorGameObjectToWorld(portalObj, hit);

                    // apply the vertical offset
                    if (verticalOffset != 0f)
                    {
                        Vector3 objPos = portalObj.transform.position;
                        //objPos.y += verticalOffset;
                        objPos += portalObj.transform.up * verticalOffset;
                        portalObj.transform.position = objPos;
                    }

                    // play portal-open animation
                    if (playAnimation != string.Empty)
                    {
                        // get reference to the portal animator
                        if (!animator)
                        {
                            animator = portalObj.GetComponent <Animator>();
                        }

                        if (animator)
                        {
                            animator.Play(playAnimation, 0, 0f);
                        }
                    }

                    // create camera rigidbody (no gravity) & box-collider, if needed
                    if (cameraBoxCollider != Vector3.zero)
                    {
                        Camera arCamera = arManager.GetMainCamera();

                        Rigidbody camRigidbody = arCamera.gameObject.GetComponent <Rigidbody>();
                        if (camRigidbody == null)
                        {
                            camRigidbody            = arCamera.gameObject.AddComponent <Rigidbody>();
                            camRigidbody.useGravity = false;
                        }

                        BoxCollider camBoxCollider = arCamera.gameObject.GetComponent <BoxCollider>();
                        if (camBoxCollider == null)
                        {
                            camBoxCollider           = arCamera.gameObject.AddComponent <BoxCollider>();
                            camBoxCollider.size      = cameraBoxCollider;
                            camBoxCollider.isTrigger = true;
                        }
                    }
                }
            }
        }
    }