c# – 如何替换位于空间的AR对象?

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

解决方法:

添加了一个方法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.");
            }
        }
    }

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

上一篇:php – 跟踪电子邮件打开时显示真实图像


下一篇:视频目标检测相关