正在关注 this教程,我可以在空间中定位一个对象。
如何用同一位置的另一个对象替换该对象?我需要一个公共(public)函数并将其分配给一个按钮,因此当我按下按钮时,“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.");
}
}
}
确保没有错别字我不是在编辑器上。
关于c# - 如何替换位于空间中的 AR 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44785940/