Hello everyone,
I need help in loading different datasets from server at runtime and create the ImageTrackers for each image in the dataset.
I have followed the instructions from Vuforia about Loading Datasets at Runtime and i get an error. Please any help or directions would be welcome.
I have attached images that display the process(as described in the tutorial) and also the error and script i am using.
The link to the tutorial from vuforia is this: https://developer.vuforia.com/forum/faq/unity-load-dataset-setup-trackables-runtime
Images:
https://www.dropbox.com/s/te5msb5ey7nq881/image1.jpg?dl=0
https://www.dropbox.com/s/9e087ni8dyb09pz/Image2.jpg?dl=0
https://www.dropbox.com/s/6mv9267p5g0m5m8/image3.jpg?dl=0
https://www.dropbox.com/s/ikvn1ermujpv4xz/image4.jpg?dl=0
Code Used:
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>"); } } }
Hello alexBasnett,
Hello alexBasnett,
Hi,
Hi,
When I tried to use that script. I am getting "NullReferenceException: Object reference not set to an instance of an object"
in the line "DataSet dataSet = objectTracker.CreateDataSet();".
Could you please help me with that?
Thanks in advance.
Hi AntoKing,
Hi AntoKing,
Usually that's a sign that you are running this code before Vuforia has initialized? Do you have delayedInitlization set and/or the VuforiaBehaviour disabled?
Thanks,
Vuforia Support
Hi,
Hi,
Thanks for your reply. We haven't set "DelayedInitialization". Also when I check the console it looks like after the "Vuforia Initialization Successful" only, script is running. Please find the attached image.
Hello again,
Hello again,
Select your ARCamera to see if the VuforiaBehviour is active. Are you running the same code as alexBasnett?
Thanks,
Vuforia Support