动态加载和激活数据集(vuforia 和 unity)

Dynamically Load and Activate a dataset (vuforia and unity)

我想制作一个系统,无需使用 "Cloud" 系统即可从我的网站动态下载各种目标。 我还想将数据集保存为 .xml 和 .dat 格式,我想从我的保存设备中激活这些格式。

有很多方法和页面可以使用 vuforia 和 unity 来做到这一点,但不幸的是,当我测试它时,我收到了所有这些方法和页面的错误。 看来我的代码有误,或者 vuforia class 被更改了。

例如请看这个link: https://developer.vuforia.com/library/articles/Solution/Unity-Load-DataSet-from-SD-Card 我收到错误:使用 Vuforia;

我将 .xml 和 .dat 文件放在 "Application.persistentDataPath + "/" + "Building1.xml" 我使用了这个脚本 "DataSetLoadBehavior" 附加了“AR 相机并将我的代码放入其中。我得到了一个错误:

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)

我的代码是这样的:

unity 4.2 专业版和 vuforia 2.8.9 或 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()
    {

    }

}

是的,Vuforia 已经改变了很多。

您现在必须将 Vuforia 作为 header 包含在内才能正常工作

using Vuforia;

希望这有效。

如果提示未找到 Vuforia,可能是因为您还没有为 Vuforia 导入 Unity 包。您可以关注这些 instructions

此外,我相信您还没有按照 Migrating your Unity Project 的步骤进行操作。新的 Vuforia 不再支持 ImageTracker,因此您必须将 ImageTracker 的所有实例更改为 ObjectTracker