繁体   English   中英

如何更换位于太空中的AR物体?

[英]How to replace an AR object, positioned in space?

教程之后,我可以将对象放在空间中。

如何在同一位置用另一个替换对象? 我需要有一个公共功能并将其分配给一个按钮,所以当我按下按钮时,“Kitty”模型将被替换为另一个模型。

这是教程中的主要脚本:

using UnityEngine;
using System.Collections;

public class KittyUIController : MonoBehaviour
{
    public GameObject m_kitten;
    private TangoPointCloud m_pointCloud;

    void Start()
    {
        m_pointCloud = FindObjectOfType<TangoPointCloud>();
    }

    void Update ()
    {
        if (Input.touchCount == 1)
        {
            // Trigger place kitten function when single touch ended.
            Touch t = Input.GetTouch(0);
            if (t.phase == TouchPhase.Ended)
            {
                PlaceKitten(t.position);
            }
        }
    }

    void PlaceKitten(Vector2 touchPosition)
    {
        // Find the plane.
        Camera cam = Camera.main;
        Vector3 planeCenter;
        Plane plane;
        if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
        {
            Debug.Log("cannot find plane.");
            return;
        }

        // Place kitten on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
        {
            Vector3 up = plane.normal;
            Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
            Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
        }
        else
        {
            Debug.Log("surface is too steep for kitten to stand on.");
        }
    }
}

添加了一个方法ReplaceKitten <= 在单击按钮时调用此方法。
增加了一个public yourOtherModel GameObject所希望的模式。
使用PlaceKitten方法使实例化小猫全局(变为变量)。

    using UnityEngine;
    using System.Collections;

    public class KittyUIController : MonoBehaviour
    {
        public GameObject m_kitten;
        public GameObject yourOtherModel; // could be prefab
        private TangoPointCloud m_pointCloud;
        private GameObject createdKitten;

        void Start()
        {
            m_pointCloud = FindObjectOfType<TangoPointCloud>();
        }

        void Update ()
        {
            if (Input.touchCount == 1)
            {
                // Trigger place kitten function when single touch ended.
                Touch t = Input.GetTouch(0);
                if (t.phase == TouchPhase.Ended)
                {
                    PlaceKitten(t.position);
                }
            }
        }
        //This method will disable the kitten and instantiate or place a GameObject on the same spot.
        public void ReplaceKitten()
        {
            GameObject modelToReplace;
            //Do this only if you will pass a Prefab to this Script
            modelToReplace = Instantiate(yourOtherModel);
            //Set your new object at the same coordinates and reset position
            modelToReplace.transform.parent = m_kitten.transform;
            modelToReplace.transform.position = new Vector3(0,0,0);
            //Disable kitten
            m_kitten.SetActive(false);
        }

        void PlaceKitten(Vector2 touchPosition)
        {
            // Find the plane.
            Camera cam = Camera.main;
            Vector3 planeCenter;
            Plane plane;
            if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
            {
                Debug.Log("cannot find plane.");
                return;
            }

            // Place kitten on the surface, and make it always face the camera.
            if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
            {
                Vector3 up = plane.normal;
                Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
                Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
                createdKitten = Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
            }
            else
            {
                Debug.Log("surface is too steep for kitten to stand on.");
            }
        }
    }

确保没有错字我不在编辑器上。

保存已实例化的对象的引用,并且每当要添加新对象时,检查实例是否为null,

如果不为null则表示对象在空间中。 销毁它并创建你想要的游戏对象的新实例并保存它的引用。

如果为null,则表示空间为空,创建一个新实例并保存其引用

 public GameObject OtherModel;   // new model that you want to replace with kitty
 GameObject myRef;               // Reference for the object that is already in scene

    void PlaceKitten(Vector2 touchPosition)
    {
        // Find the plane.
        Camera cam = Camera.main;
        Vector3 planeCenter;
        Plane plane;
        if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane))
        {
            Debug.Log("cannot find plane.");
            return;
        }

        // Place kitten on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
        {
            Vector3 up = plane.normal;
            Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            Vector3 forward = Vector3.Cross(right, plane.normal).normalized;

            // Check whether we have already placed something or not
            if (myRef != null)
            {
                Transform temp = myRef.transform;
                Destroy(myRef);
                myRef = Instantiate(OtherModel, temp.position, temp.rotation);
            }
            else
            {
                myRef = Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
            }
        }
        else
        {
            Debug.Log("surface is too steep for kitten to stand on.");
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM