- Sort Posts
- 10 replies
- Last post
replace 3d model runtime
Note that the significant
Note that the significant step is the assignment of the obj.transform.parent. You don't need to reference the trackable using the dataset, you can simply obtain it as a gameobject.
Here's how to instantiate custom models in Unity - http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html
Another approach is to pre-load the models and enable / disable their rendering at runtime. The DefaultTrackableEventHandler demonstrates this approach and shows you how to control their colliders as well.
Error in using this code
Hello David,
Thank you for sharing this code. I want to use this to load multiple models with a gui texture button. I am new to using C# and unable to resolve the errors. I get the following errors when I save your code in a script file.
Type `ImageTracker' does not contain a definition for `GetActiveDataSet' and no extension method `GetActiveDataSet' of type `ImageTracker' could be found (are you missing a using directive or an assembly reference?)
Type `DataSet' does not contain a definition for `GetNumTrackables' and no extension method `GetNumTrackables' of type `DataSet' could be found (are you missing a using directive or an assembly reference?)
How do I resolve these errors by specifying the right directive? I thought of using DataSetTrackableBehaviour class but I do not know how to do it in C#.
I created the entire app with separate levels but then it loads very slow each time when changing levels. Hence I am trying to load and unload each model with this method. Other option of preloading all 20+ models at scene startup may slow down scene loading so I can not use it.
Please guide me.
Thank you.
Hi ksb24, the code you are
Hi ksb24,
the code you are referring below was working in 1.5, but is now obsolete in 2.0, that's why you get the errors.
You can either try to upgrade it (see migratio guide: https://developer.vuforia.com/resources/dev-guide/migrating-your-unity-project )
or you can more simply try to retrieve a Trackable just by using the Unity function GameObject.Find( "MyTrackableName" );
The specific approach is also depending on your application logic.
I hope this helps.
Hi, there are many ways of doing that;
one possible way is to attach a script to the AR camera, which do a model swaps based on some input events;
in general, the previous models attached as children of a trackable can be disabled, while the new model can be parented under the same trackable;
This is an example of a script that replaces any pre-existing model with a simple cube (the swap here is activated by a simple touch on the screen, but this is just for simplicity of the example);
you can use this sample as a starting point and elaborate more sophisticated solutions on your own:
public class ModelSwapper : MonoBehaviour {
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonUp(0)) {
SwapModel();
}
}
private void SwapModel() {
ImageTracker tracker = (ImageTracker)TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER);
if (tracker == null) return;
DataSet activeDataSet = tracker.GetActiveDataSet();
if (activeDataSet != null && activeDataSet.GetNumTrackables() > 0) {
DataSetTrackableBehaviour trackable = activeDataSet.GetTrackable(0);
GameObject trackableGameObject = trackable.gameObject;
//disable previous children (old 3d models)
for (int i = 0; i < trackableGameObject.transform.GetChildCount(); i++) {
Transform child = trackableGameObject.transform.GetChild(i);
child.gameObject.active = false;
}
//Create cube object and parent it to trackable gameObject
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = trackable.transform;
cube.transform.localPosition = new Vector3(0,0.2f,0);
cube.transform.localRotation = Quaternion.identity;
cube.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
cube.active = true;
}
}
}