In a Unity scene, attach the script SimpleModelTargetManager.cs to the Vuforia AR Camera GameObject. Import your Model Target Unity Asset Package; you must point to the relative path of the dataset files (DAT & XML).
The script below creates and loads a Model Target Observer when Vuforia is initialized.
usingUnityEngine;usingVuforia;publicclassSimpleModelTargetManager:MonoBehaviour{stringdataSetPath="Vuforia/Vuforia_RZR_Toy.xml";stringtargetName="Vuforia_RZR_Toy";// Start is called before the first frame update voidStart(){VuforiaApplication.Instance.OnVuforiaInitialized+=OnVuforiaInitialized;VuforiaApplication.Instance.OnVuforiaStarted+=OnVuforiaStarted;}voidOnVuforiaInitialized(VuforiaInitErrorerror){if(error==VuforiaInitError.NONE)return;}// Load and create Observer from the given path. voidOnVuforiaStarted(){// Create a Model Target from the database. varmModelTarget=VuforiaBehaviour.Instance.ObserverFactory.CreateModelTarget(dataSetPath,targetName);mModelTarget.OnTargetStatusChanged+=OnTargetStatusChanged;}voidOnTargetStatusChanged(ObserverBehaviourbehaviour,TargetStatusstatus){Debug.Log($"target status: {status.Status}");}}
If your Model Target database has multiple Model Targets, you can create all Model Targets from one database in one go.
usingUnityEngine;usingVuforia;publicclassMutlipleModelTargetLoader:MonoBehaviour{staticreadonlystring[]targetNames=new[]{"Vuforia_MarsLander","Vuforia_RZR_Toy"};// Start is called before the first frame update voidStart(){VuforiaApplication.Instance.OnVuforiaInitialized+=OnVuforiaInitialized;}voidOnVuforiaInitialized(VuforiaInitErrorerror){if(error==VuforiaInitError.NONE)OnVuforiaStarted();}// Load and create Observer from the given path. voidOnVuforiaStarted(){// Create a Model Target for each target name in the database foreach(vartargetintargetNames){varmtBehaviour=VuforiaBehaviour.Instance.ObserverFactory.CreateModelTarget("Vuforia/Vuforia_Marslander_RZR_Toy_Advanced.xml",target);Debug.Log("target created: "+target);mtBehaviour.OnTargetStatusChanged+=OnTargetStatusChanged;}}voidOnTargetStatusChanged(ObserverBehaviourbehaviour,TargetStatusstatus){Debug.Log($"target status: {status.Status}");}}
Switch between Model Targets from the same Database¶
By default, tracking will switch between each activated Model Target that is detected. Tracking multiple Model Targets simultaneously is not supported. In scenarios where you wish to track Model Targets in a sequence, you can activate and deactivate them either as GameObjects or by getting the Model Target component and setting enabled = false;
usingUnityEngine;usingVuforia;publicclassSimpleSelectGuideView:MonoBehaviour{// Reference to the ModelTargetBehaviour componentpublicModelTargetBehaviourmodelTargetBehaviour;// Index of the Guide View to be selectedpublicintguideViewIndex=0;voidStart(){VuforiaApplication.Instance.OnVuforiaStarted+=OnVuforiaStarted;}voidOnVuforiaStarted(){if(modelTargetBehaviour!=null){// Check if the Guide View index is validif(guideViewIndex>=0&&guideViewIndex<modelTargetBehaviour.GetNumGuideViews()){// Set the active Guide ViewmodelTargetBehaviour.SetActiveGuideViewIndex(guideViewIndex);Debug.Log("Guide View "+guideViewIndex+" selected.");}else{Debug.LogError("Invalid Guide View index.");}}else{Debug.LogError("ModelTargetBehaviour component not found.");}}}
usingUnityEngine;usingUnityEngine.UI;usingVuforia;publicclassSelectGuideView:MonoBehaviour{// Reference to the ModelTargetBehaviour componentpublicModelTargetBehaviourmodelTargetBehaviour;// Reference to the Button componentpublicButtonswitchButton;// Current Guide View indexprivateintcurrentGuideViewIndex=0;// Total number of Guide ViewsprivateinttotalGuideViews=0;voidStart(){VuforiaApplication.Instance.OnVuforiaStarted+=OnVuforiaStarted;}voidOnVuforiaStarted(){if(modelTargetBehaviour!=null){// Get the total number of Guide ViewstotalGuideViews=modelTargetBehaviour.GetNumGuideViews();// Set the initial Guide ViewmodelTargetBehaviour.SetActiveGuideViewIndex(currentGuideViewIndex);// Add a listener to the Button componentswitchButton.onClick.AddListener(SwitchGuideView);}else{Debug.LogError("ModelTargetBehaviour component not found.");}}voidSwitchGuideView(){if(modelTargetBehaviour!=null){// Increment the current Guide View indexcurrentGuideViewIndex++;// If the current Guide View index is out of range, reset it to the first Guide Viewif(currentGuideViewIndex>=totalGuideViews){currentGuideViewIndex=0;}// Set the active Guide ViewmodelTargetBehaviour.SetActiveGuideViewIndex(currentGuideViewIndex);Debug.Log("Switched to Guide View "+currentGuideViewIndex);}else{Debug.LogError("ModelTargetBehaviour component not found.");}}}