سؤال

I am developing a Vuforia AR app using Unity 3D. I want to change the model being augmented with other based on a UI action.

Below code is working. See the attached screenshot. In chips target cow & elephant models are showing. I drag the ModelSwapper script to ARCamera and in Inspector view i choosed cow. When i run the app click the button the cow is gone. It's works fine. Second screenshot tarmac showing cow & elephant model. In inspector what should i do? I need to remove elephant while click the button. Is i need to create another script for 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;

            }

        }

    }

enter image description here

enter image description here

هل كانت مفيدة؟

المحلول

In your removing the "pre-augmentations" you remove everything from the child of the target, but the Elephant is not the child of the Cow. Get a reference to the elephant and then you can either disable it with SetActive or remove completely with Destroy.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top