Log in or register to post comments

Dynamically Load and Activate a dataset (vuforia and unity)

April 21, 2015 - 12:11am #1

Hi
i want to make a system that i can download various target dynamically from my website without using "Cloud" system, also i want to save dataset to .xml and .dat format and i can active them 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 have received Error message for all of them.
it seems that i have made a mistake in my code or vuforia class was changed.
For instance please look this link:
https://developer.vuforia.com/library/articles/Solution/Unity-Load-DataSet-from-SD-Card
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()
    {
        
    }

}

 

Dynamically Load and Activate a dataset (vuforia and unity)

April 18, 2017 - 6:46am #7

Im getting same error 

How to fix it. explain your answer

Dynamically Load and Activate a dataset (vuforia and unity)

May 28, 2016 - 11:04pm #6

could you please explain how you fixed this error.

Dynamically Load and Activate a dataset (vuforia and unity)

July 22, 2015 - 8:07pm #5

Thanks !

Got it !

Must call it after Vuforia has initialized

Dynamically Load and Activate a dataset (vuforia and unity)

July 22, 2015 - 1:23pm #4

It might be a timing issue. Try registering a callback method to be run after Vuforia has initialized. See this FAQ for an example:

https://developer.vuforia.com/forum/faq/unity-load-dataset-setup-trackables-runtime

 

Dynamically Load and Activate a dataset (vuforia and unity)

July 19, 2015 - 8:45pm #3

i have got an error when i create a new empty dataset like this

		// Request an ImageTracker instance from the TrackerManager.
		ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker> ();

		// Create a new empty data set.
		DataSet dataSet = objectTracker.CreateDataSet ();

it throws

NullReferenceException: Object reference not set to an instance of an object

i copy the code from your library but throws exception=.=

Please hlep 

Thanks.

Dynamically Load and Activate a dataset (vuforia and unity)

April 22, 2015 - 7:14pm #2

The Vuforia namespace was introduced with Vuforia 4.x:

using Vuforia;  // Needed if using Vuforia 4.x

For Vuforia 3.x and prior versions, go ahead and remove it.

 

Log in or register to post comments