2013-12-20 60 views
0

我正在開發使用Unity 3D的Vuforia AR應用程序。我想根據UI操作更改其他模型。使用Vuforia和Unity交換3D模型

下面的代碼正在工作。請參閱附件截圖。在籌碼目標牛&大象模型正在顯示。我將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; 

      } 

     } 

    } 

enter image description here

enter image description here

+1

我可以推薦你檢查[this](http://answers.unity3d.com/index.html)鏈接,你可能會發現你的答案更快。 –

+1

@ShaunWild它取決於。最快的方法是X張貼;-) – Kay

+0

@凱,非常真實 –

回答

0

在你刪除了「預增廣」從目標的孩子刪除一切,但大象不是的孩子。獲取一個參考的大象,然後你可以通過SetActive禁用它或完全刪除與Destroy

+0

我不明白。 – user3073276