C# 使用Vuforia和Unity交换三维模型
我正在使用Unity 3D开发Vuforia AR应用程序。我想根据一个UI操作更改正在使用其他方法扩展的模型 下面的代码正在工作。请参阅附带的屏幕截图。在芯片中,目标是奶牛和大象模型。我将ModelSwapper脚本拖到ARCamera,并在Inspector视图中选择了cow。当我运行应用程序时,点击按钮,奶牛就不见了。它很好用。第二个屏幕截图展示了奶牛和大象模型。我该怎么办?我需要在点击按钮的同时移除大象。我需要为tarmac创建另一个脚本吗C# 使用Vuforia和Unity交换三维模型,c#,unity3d,vuforia,C#,Unity3d,Vuforia,我正在使用Unity 3D开发Vuforia AR应用程序。我想根据一个UI操作更改正在使用其他方法扩展的模型 下面的代码正在工作。请参阅附带的屏幕截图。在芯片中,目标是奶牛和大象模型。我将ModelSwapper脚本拖到ARCamera,并在Inspector视图中选择了cow。当我运行应用程序时,点击按钮,奶牛就不见了。它很好用。第二个屏幕截图展示了奶牛和大象模型。我该怎么办?我需要在点击按钮的同时移除大象。我需要为tarmac创建另一个脚本吗 using UnityEngine;
using UnityEngine;
using System.Collections;
public class ModelSwapper : MonoBehaviour {
// public TrackableBehaviour theTrackable;
private GameObject theTrackable;
private GameObject cube;
private bool mSwapModel = false;
public Transform MyPrefab;
// Use this for initialization
void Start () {
theTrackable=GameObject.Find("Cow");
cube = GameObject.Find("Cow");
if (theTrackable == null)
{
Debug.Log ("Warning: Trackable not set !!");
}
}
// Update is called once per frame
void Update () {
if (mSwapModel && theTrackable != null) {
SwapModel();
mSwapModel = false;
}
}
void OnGUI() {
if (GUI.Button (new Rect(50,50,120,40), "Swap Model")) {
mSwapModel = true;
}
}
private void SwapModel()
{
GameObject trackableGameObject = theTrackable.gameObject;
//disable any pre-existing augmentation
for (int i = 0; i < trackableGameObject.transform.GetChildCount(); i++)
{
Transform child = trackableGameObject.transform.GetChild(i);
child.gameObject.active = false;
}
// Create a simple cube object
//GameObject model = GameObject.CreatePrimitive(PrimitiveType.Cube);
if (MyPrefab != null)
{
Transform model = GameObject.Instantiate(MyPrefab) as Transform;
// Re-parent the model as child of the trackable gameObject
model.parent = theTrackable.transform;
// Adjust the position and scale
// so that it fits nicely on the target
model.transform.localPosition = new Vector3(0,0.2f,0);
model.transform.localRotation = Quaternion.identity;//you might need to adjust the rotation
model.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
// Make sure it is active
model.active = true;
}
}
}
使用UnityEngine;
使用系统集合;
公共类ModelSwapper:MonoBehavior{
//公众可追踪的行为;
可追踪的私人游戏对象;
私有游戏对象立方体;
私有bool mSwapModel=false;
公共场所;
//用于初始化
无效开始(){
theTrackable=GameObject.Find(“Cow”);
cube=GameObject.Find(“Cow”);
如果(可跟踪==null)
{
Log(“警告:未设置可跟踪!!”);
}
}
//每帧调用一次更新
无效更新(){
if(mSwapModel&&theTrackable!=null){
SwapModel();
mSwapModel=false;
}
}
void OnGUI(){
if(GUI.Button(新Rect(50,50120,40),“交换模型”)){
mSwapModel=true;
}
}
私有void SwapModel()
{
GameObject Trackable GameObject=Trackable.GameObject;
//禁用任何预先存在的扩充
对于(int i=0;i
在删除“预增强”时,您删除了目标子对象的所有内容,但大象不是奶牛的子对象。获取大象的引用,然后您可以使用
SetActive
禁用它,或者使用Destroy
完全删除它。我建议您签出链接,您可以更快地找到答案。@shaunbled这取决于具体情况。最快的方法是X-POST;-)