首页 / C# / c# – 如何替换位于空间的AR对象?
c# – 如何替换位于空间的AR对象?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何替换位于空间的AR对象?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3078字,纯文字阅读大概需要5分钟。
内容图文
![c# – 如何替换位于空间的AR对象?](/upload/InfoBanner/zyjiaocheng/781/7e86c2bb0a1446149db74b614827dfda.jpg)
在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.");
}
}
}
确保没有错字我不在编辑器上.
内容总结
以上是互联网集市为您收集整理的c# – 如何替换位于空间的AR对象?全部内容,希望文章能够帮你解决c# – 如何替换位于空间的AR对象?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。