我想制作一个可用于从我的网站动态下载各种目标的系统,而无需使用“云”系统。
我也想将数据集保存为.xml和.dat格式,然后从保存设备中激活它们。

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

例如,请查看此链接:
https://developer.vuforia.com/library/articles/Solution/Unity-Load-DataSet-from-SD-Card
我得到了错误:使用Vuforia。

我将.xml和.dat文件放在“ Application.persistentDataPath +” /“ +” Building1.xml“中
我使用此脚本“ DataSetLoadBehavior”,该脚本附加了“ AR摄像机并将代码放入其中。我收到一个错误:


  NullReferenceException:对象引用未设置为的实例
  对象DataSetLoadBehaviour.OnInitialized()(在Assets / Qualcomm
  增强现实/脚本/DataSetLoadBehaviour.cs:49)
  DataSetLoadBehaviour.Start()(在Assets / Qualcomm Augmented中
  现实/脚本/DataSetLoadBehaviour.cs:80)


我的代码是这样的:

unity 4.2 pro和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作为标题,才能使其正常工作

using Vuforia;


希望这行得通。

如果显示未找到Vuforia,则可能是因为您尚未导入Vuforia的Unity软件包。您可以遵循这些instructions

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

关于c# - 动态加载和激活数据集(Vuforia和Unity),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29765840/

10-15 07:50