我正在使用Unity 3D开发Vuforia AR应用程序。我想基于UI操作更改使用其他模型进行扩充的模型。
以下代码正常运行。请参阅附带的屏幕截图在筹码目标牛和&大象模特正在展示。我将ModelSwapper脚本拖到ARCamera,在Inspector视图中我选择了cow。当我运行应用程序时,单击按钮,牛已经消失。它工作正常。第二个屏幕截图柏油碎石机显示牛和&大象模型。在检查员,我该怎么办?我需要在点击按钮的同时移除大象。我需要为tarmac创建另一个脚本吗?
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;
}
}
}
答案 0 :(得分:0)
在删除“预扩充”时,您会移除目标儿童的所有内容,但大象不是 Cow 的子级。获取参考给大象,然后您可以使用SetActive
停用它,或者使用Destroy
完全删除。