Model Target Runtime Scripting

Use the Vuforia Engine C# API to create and load Model Targets at runtime. Switch between Guide Views or render a 3D Guide View for eyewear devices.

See Introduction to Model Targets in Unity for setup instructions and importing Model Targets.

See also Model Target runtime meshes for loading its mesh at runtime for occlusion and collider.

Load Model Targets at Runtime

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.

12345678910111213141516171819202122232425262728293031323334353637
Copy
using UnityEngine; using Vuforia; public class SimpleModelTargetManager : MonoBehaviour { string dataSetPath = "Vuforia/Vuforia_RZR_Toy.xml"; string targetName = "Vuforia_RZR_Toy"; // Start is called before the first frame update void Start() { VuforiaApplication.Instance.OnVuforiaInitialized += OnVuforiaInitialized; VuforiaApplication.Instance.OnVuforiaStarted += OnVuforiaStarted; } void OnVuforiaInitialized(VuforiaInitError error) { if (error == VuforiaInitError.NONE) return; } // Load and create Observer from the given path. void OnVuforiaStarted() { // Create a Model Target from the database. var mModelTarget = VuforiaBehaviour.Instance.ObserverFactory.CreateModelTarget( dataSetPath, targetName); mModelTarget.OnTargetStatusChanged += OnTargetStatusChanged; } void OnTargetStatusChanged(ObserverBehaviour behaviour, TargetStatus status) { 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.

12345678910111213141516171819202122232425262728293031323334353637
Copy
using UnityEngine; using Vuforia; public class MutlipleModelTargetLoader : MonoBehaviour { static readonly string[] targetNames = new[] { "Vuforia_MarsLander", "Vuforia_RZR_Toy" }; // Start is called before the first frame update void Start() { VuforiaApplication.Instance.OnVuforiaInitialized += OnVuforiaInitialized; } void OnVuforiaInitialized(VuforiaInitError error) { if (error == VuforiaInitError.NONE) OnVuforiaStarted(); } // Load and create Observer from the given path. void OnVuforiaStarted() { // Create a Model Target for each target name in the database foreach (var target in targetNames) { var mtBehaviour = VuforiaBehaviour.Instance.ObserverFactory.CreateModelTarget("Vuforia/Vuforia_Marslander_RZR_Toy_Advanced.xml", target); Debug.Log("target created: " + target); mtBehaviour.OnTargetStatusChanged += OnTargetStatusChanged; } } void OnTargetStatusChanged(ObserverBehaviour behaviour, TargetStatus status) { 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;

12345678910111213141516171819202122
Copy
using System.Linq; using UnityEngine; using UnityEngine.UI; using Vuforia; public class SwitchModelTarget : MonoBehaviour { public GameObject mActive; public GameObject mInactive; public Button button; void Start() { button.onClick.AddListener(SwitchModelTargetTrigger); } void SwitchModelTargetTrigger() { mInactive.SetActive(false); mActive.SetActive(true); } }

Render 2D Guide Views

If the Model Target has one or more Guide Views, you can load and render one like this:

1234567891011121314151617181920212223242526272829303132333435363738
Copy
using UnityEngine; using Vuforia; public class SimpleSelectGuideView : MonoBehaviour { // Reference to the ModelTargetBehaviour component public ModelTargetBehaviour modelTargetBehaviour; // Index of the Guide View to be selected public int guideViewIndex = 0; void Start() { VuforiaApplication.Instance.OnVuforiaStarted += OnVuforiaStarted; } void OnVuforiaStarted() { if (modelTargetBehaviour != null) { // Check if the Guide View index is valid if (guideViewIndex >= 0 && guideViewIndex < modelTargetBehaviour.GetNumGuideViews()) { // Set the active Guide View modelTargetBehaviour.SetActiveGuideViewIndex(guideViewIndex); Debug.Log("Guide View " + guideViewIndex + " selected."); } else { Debug.LogError("Invalid Guide View index."); } } else { Debug.LogError("ModelTargetBehaviour component not found."); } } }

Switch between 2D Guide Views

If there are multiple Guide Views, you can add a button to cycle through them or create logic that changes the Guide View based on conditions.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
Copy
using UnityEngine; using UnityEngine.UI; using Vuforia; public class SelectGuideView : MonoBehaviour { // Reference to the ModelTargetBehaviour component public ModelTargetBehaviour modelTargetBehaviour; // Reference to the Button component public Button switchButton; // Current Guide View index private int currentGuideViewIndex = 0; // Total number of Guide Views private int totalGuideViews = 0; void Start() { VuforiaApplication.Instance.OnVuforiaStarted += OnVuforiaStarted; } void OnVuforiaStarted() { if (modelTargetBehaviour != null) { // Get the total number of Guide Views totalGuideViews = modelTargetBehaviour.GetNumGuideViews(); // Set the initial Guide View modelTargetBehaviour.SetActiveGuideViewIndex(currentGuideViewIndex); // Add a listener to the Button component switchButton.onClick.AddListener(SwitchGuideView); } else { Debug.LogError("ModelTargetBehaviour component not found."); } } void SwitchGuideView() { if (modelTargetBehaviour != null) { // Increment the current Guide View index currentGuideViewIndex++; // If the current Guide View index is out of range, reset it to the first Guide View if (currentGuideViewIndex >= totalGuideViews) { currentGuideViewIndex = 0; } // Set the active Guide View modelTargetBehaviour.SetActiveGuideViewIndex(currentGuideViewIndex); Debug.Log("Switched to Guide View " + currentGuideViewIndex); } else { Debug.LogError("ModelTargetBehaviour component not found."); } } }

Can this page be better?
Share your feedback via our issue tracker