Load and activate dataset dynamically (wuforia and unity)
I want to create a system that I can use to dynamically download various objects from my site without using the "Cloud" system. I also want to save the dataset in .xml and .dat formats that I want to activate from my saving device.
There are many methods and pages that can be done with vuforia and unity, but unfortunately when I test it I get an error for all of them. It seems that I made a mistake in my code or the vuforia class has changed.
For example, check out this link: https://developer.vuforia.com/library/articles/Solution/Unity-Load-DataSet-from-SD-Card I got an error: Using Vuforia;
I put the .xml and .dat files in "Application.persistentDataPath +" / "+" Building1.xml "I used this Script" DataSetLoadBehavior "which bound the" AR camera "and put my code in it. I got the error:
NullReferenceException: Object reference not set to DataSetLoadBehaviour.OnInitialized () object instance (in Assets / Qualcomm Augmented Reality / Scripts / DataSetLoadBehaviour.cs: 49) DataSetLoadBehaviour.Start () (in Assets / Qualcomm. Augmented Reality / DataSetL script cs: 80)
My code:
unit 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()
{
}
}
source to share
Yes, Vuforia has changed a lot.
Now you need to include Vuforia
as header for it to work
using Vuforia;
Hope it works.
If it says that Vuforia was not found, it is probably because you did not import the Unitypackage for Vuforia. You can follow these instructions .
Also, I assume that you did not follow the steps to Migrate your Unity project . New Vuforia doesn't support anymore ImageTracker
, so you will need to change all instances ImageTracker
toObjectTracker
source to share