c# - Dynamically Load and Activate a dataset (vuforia and unity)


Keywords:c# 


Question: 

I want to make a system that I can use to download various targets dynamically from my website without using "Cloud" system. I also want to save the dataset to .xml and .dat formats which I want to activate from my saving device.

There are a lot of methods and pages to doing that with vuforia and unity, but unfortunately when I test it I receive an error for all of them. It seems that i have made a mistake in my code or a vuforia class was changed.

For instance please look this link: I got Error: Using Vuforia;

I placed the .xml and .dat files in "Application.persistentDataPath + "/" + "Building1.xml" i used this Script "DataSetLoadBehavior" that attached "AR Camera and placed my code in it. I got an Error:

NullReferenceException: Object reference not set to an instance of an object DataSetLoadBehaviour.OnInitialized () (at Assets/Qualcomm Augmented Reality/Scripts/DataSetLoadBehaviour.cs:49) DataSetLoadBehaviour.Start () (at Assets/Qualcomm Augmented Reality/Scripts/DataSetLoadBehaviour.cs:80)

My code is this:

unity 4.2 pro and vuforia 2.8.9 or 3.0.9

/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Qualcomm Connected Experiences, Inc.
==============================================================================*/

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// This behaviour allows to automatically load and activate one or more DataSet on startup
/// </summary>
public class DataSetLoadBehaviour : DataSetLoadAbstractBehaviour
{
       [HideInInspector, SerializeField]
       public List<string> mDataSetsToActivate2 = new List<string>();
      [SerializeField, HideInInspector]
    public List<string> mDataSetsToLoad2 = new List<string>();

    protected DataSetLoadBehaviour()
    {
    }
    private void OnDestroy()
    {
        QCARAbstractBehaviour behaviour = (QCARAbstractBehaviour) UnityEngine.Object.FindObjectOfType(typeof(QCARAbstractBehaviour));
        if (behaviour != null)
        {

        }
    }

    public void OnInitialized()
    {

        if (QCARRuntimeUtilities.IsQCAREnabled())
        {
            foreach (string str in this.mDataSetsToLoad2)
            {

                if (!DataSet.Exists(str, QCARUnity.StorageType.STORAGE_ABSOLUTE))

                {
                    Debug.LogError("Data set " + str + " does not exist.");
                }
                else
                {

                    ImageTracker tracker = TrackerManager.Instance.GetTracker<ImageTracker>();
                    DataSet dataSet = tracker.CreateDataSet();
                    if (!dataSet.Load(str))
                    {
                        Debug.LogError("Failed to load data set " + str + ".");
                    }
                    else if (this.mDataSetsToActivate2.Contains(str))
                    {
                        tracker.ActivateDataSet(dataSet);
                    }
                }
            }
        }
    }

    public void OnTrackablesUpdated()
    {
    }

    private void Start()
    {

        QCARAbstractBehaviour behaviour = (QCARAbstractBehaviour) UnityEngine.Object.FindObjectOfType(typeof(QCARAbstractBehaviour));
        if (behaviour != null)
        {


            mDataSetsToLoad2.Add(Application.persistentDataPath + "/" + "Building1.xml");




            OnInitialized();
        }
    }
    public override void AddOSSpecificExternalDatasetSearchDirs()
    {
#if UNITY_ANDROID
        if (Application.platform == RuntimePlatform.Android)
        {
            // Get the external storage directory
            AndroidJavaClass jclassEnvironment = new AndroidJavaClass("android.os.Environment");
            AndroidJavaObject jobjFile = jclassEnvironment.CallStatic<AndroidJavaObject>("getExternalStorageDirectory");
            string externalStorageDirectory = jobjFile.Call<string>("getAbsolutePath");

            // Get the package name
            AndroidJavaObject jobjActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
            string packageName = jobjActivity.Call<string>("getPackageName");

            // Add some best practice search directories
            //
            // Assumes just Vufroria datasets extracted to the files directory
            AddExternalDatasetSearchDir(externalStorageDirectory + "/Android/data/" + packageName + "/files/");
            // Assume entire StreamingAssets dir is extracted here and our datasets are in the "QCAR" directory
            AddExternalDatasetSearchDir(externalStorageDirectory + "/Android/data/" + packageName + "/files/QCAR/");
        }
#endif //UNITY_ANDROID
    }

    void Update()
    {

    }

}

1 Answer: 

Yeah, Vuforia has has changed a lot.

You will now have to include Vuforia as a header in order for it to work

using Vuforia;

Hope this works.

If it says Vuforia hasn't been found it's probably because you haven't imported the Unitypackage for Vuforia. You can follow these instructions.

Also, I believe you haven't followed the steps to Migrating your Unity Project. The new Vuforia doesn't support ImageTracker anymore, hence you will have to change all instances of ImageTracker to ObjectTracker