【发布时间】:2013-12-20 07:59:40
【问题描述】:
我正在使用 Unity 3D 开发 Vuforia AR 应用。我想根据 UI 操作更改正在增强的模型。
以下代码正在运行。请参阅随附的屏幕截图。芯片中的目标牛和大象模型正在展示。我将 ModelSwapper 脚本拖到 ARCamera 并在 Inspector 视图中选择了奶牛。当我运行应用程序时,单击按钮牛不见了。它工作正常。第二张截图展示了牛和大象模型的停机坪。在检查员我该怎么办?我需要在单击按钮时移除大象。我需要为停机坪创建另一个脚本吗?
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;
}
}
}
【问题讨论】:
-
我建议您查看this链接,您可能会更快找到答案。
-
@ShaunWild 视情况而定。最快的方法是 X-posting ;-)