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

时间:2017-06-27 16:59:14

标签: c# unity3d tracking augmented-reality google-project-tango

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

如何在同一位置更换另一个物体?我需要有一个公共功能并将其分配给一个按钮,所以当我按下按钮时," 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.");
        }
    }
}

2 个答案:

答案 0 :(得分:2)

添加了一种方法ReplaceKitten&lt; = 点击按钮后调用此方法。
为所需型号添加了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.");
            }
        }
    }

确保没有拼写错误我不在编辑器上。

答案 1 :(得分:0)

保存已实例化的对象的引用,并且每当要添加新对象时,检查该实例是否为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.");
        }
    }