The following Unity script can be used to load a DataSet at runtime, activate it's Trackables, and attach an augmentation object of your choice. The script can be attached to an empty GameObject in a Vuforia scene that has at least one dataset which is not already "Loaded" and "Activated" in the ARCamera object Inspector settings. The script has two public members which can be set in the Inspector:
- Augmentation Object
- Data Set Name
Steps: Attach the script to an empty GameObject in your scene and drag an object from your scene to be used as the augmentation into the first field. Type the name of your dataset in the second field.
The script will rename all of the automatically generated New Game Object objects that have ImageTargetBehaviours with the name of the Trackable in the DataSet and add DefaultTrackableEventHandler and TurnOffBehaviour script components to them. It will also instantiate the chosen augmentation and attach it to the Trackable.
Image shows the renamed objects as they appear in Hierarchy during PlayMode:
Notes: While you can have more than 100 targets loaded, ObjectTracker cannot have more than 100 total targets activated. You might encounter this limit if you have multiple datasets with targets totalling more than 100. You will get a "Could not activate dataset." error in the Unity console if the number of targets in the dataset you are attempting to activate will increase the number of currently active targets beyond 100. Update: The 100 target limit was increased to 1000 in Vuforia 5.0.5. See: Best-practices-for-large-device-databases
DynamicDataSetLoader.cs
using UnityEngine; using System.Collections; using Vuforia; using System.Collections.Generic; public class DynamicDataSetLoader : MonoBehaviour { // specify these in Unity Inspector public GameObject augmentationObject = null; // you can use teapot or other object public string dataSetName = ""; // Assets/StreamingAssets/QCAR/DataSetName // Use this for initialization void Start() { // Vuforia 5.0 to 6.1 VuforiaBehaviour vb = GameObject.FindObjectOfType<VuforiaBehaviour>(); vb.RegisterVuforiaStartedCallback(LoadDataSet); // Vuforia 6.2+ VuforiaARController.Instance.RegisterVuforiaStartedCallback(LoadDataSet); } void LoadDataSet() { ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>(); DataSet dataSet = objectTracker.CreateDataSet(); if (dataSet.Load(dataSetName)) { objectTracker.Stop(); // stop tracker so that we can add new dataset if (!objectTracker.ActivateDataSet(dataSet)) { // Note: ImageTracker cannot have more than 100 total targets activated Debug.Log("<color=yellow>Failed to Activate DataSet: " + dataSetName + "</color>"); } if (!objectTracker.Start()) { Debug.Log("<color=yellow>Tracker Failed to Start.</color>"); } int counter = 0; IEnumerable<TrackableBehaviour> tbs = TrackerManager.Instance.GetStateManager().GetTrackableBehaviours(); foreach (TrackableBehaviour tb in tbs) { if (tb.name == "New Game Object") { // change generic name to include trackable name tb.gameObject.name = ++counter + ":DynamicImageTarget-" + tb.TrackableName; // add additional script components for trackable tb.gameObject.AddComponent<DefaultTrackableEventHandler>(); tb.gameObject.AddComponent<TurnOffBehaviour>(); if (augmentationObject != null) { // instantiate augmentation object and parent to trackable GameObject augmentation = (GameObject)GameObject.Instantiate(augmentationObject); augmentation.transform.parent = tb.gameObject.transform; augmentation.transform.localPosition = new Vector3(0f, 0f, 0f); augmentation.transform.localRotation = Quaternion.identity; augmentation.transform.localScale = new Vector3(0.005f, 0.005f, 0.005f); augmentation.gameObject.SetActive(true); } else { Debug.Log("<color=yellow>Warning: No augmentation object specified for: " + tb.TrackableName + "</color>"); } } } } else { Debug.LogError("<color=yellow>Failed to load dataset: '" + dataSetName + "'</color>"); } } }
Related Articles:
- How To Load and Activate Multiple Device Databases at Runtime
- How To Load Datasets at Runtime in native Android and iOS Projects
Other DataSet FAQs / Articles:
- Unity - Loading DataSet from SD Card
- Unity - Scene swap and DataSet persistence
- Android - How do I replace the DataSet in Image Targets sample
- Technical - How can I remove a Target from a Dataset
- Dataset Schema Specification
Attachment | Size |
---|---|
![]() | 13.94 KB |
![]() | 7.05 KB |