【问题标题】:Swap 3D model using Vuforia and Unity使用 Vuforia 和 Unity 交换 3D 模型
【发布时间】: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 ;-)

标签: c# unity3d vuforia


【解决方案1】:

在您删除“预增强”时,您会从目标的孩子身上删除所有内容,但 大象 不是 牛 的孩子。获取大象的reference,然后您可以使用SetActive 禁用它或使用Destroy 完全删除它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    相关资源
    最近更新 更多