Vuforia Cloud云加载/沙盒路径加载识别数据实现热更新
1. Cloud 云加载方式
中国不支持,会无法创建CludeDataBase ,注册为其他国家才行,但即使注册成功,识别的速度也是相当慢,不建议使用Cloud 方式。具体使用在Untiy商场看vuforia的Smaple 案例
教程
//https://www.youtube.com/watch?v=9NiB34ojl0A
//https://www.youtube.com/watch?v=7XGuzsNvDIs
2.使用沙河路径的方式
注意 :不包含打包,下载 只有从沙河中加载数据
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Vuforia;
public class DataSetHandle : MonoBehaviour {
public static DataSet dataSet = null;
//沙河路径加载/Vuforia加载程序集
//string path = Application.persistentDataPath + "/Vuforia/" +absolutePath+ ".xml";
public static void LoadDataSetFromPath(string absolutePath)
{
ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
if (!DataSet.Exists(absolutePath, VuforiaUnity.StorageType.STORAGE_ABSOLUTE))
{
Debug.Log("load dataset Exists null");
return;
}
else
{
objectTracker.Stop();
dataSet = objectTracker.CreateDataSet();
if (dataSet.Load(absolutePath, VuforiaUnity.StorageType.STORAGE_ABSOLUTE))
{
//这里必须要停止跟踪才能激活DataSet
objectTracker.ActivateDataSet(dataSet);
}
objectTracker.Start();
}
}
//默认的是从StreamingAssets/Vuforia加载程序集
public static void LoadDataSet(string dataSetName)
{
ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
objectTracker.Stop();
DataSet dataSet = objectTracker.CreateDataSet();
if (dataSet.Load(dataSetName))
{
//这里必须要停止跟踪才能激活DataSet
objectTracker.ActivateDataSet(dataSet);
}
objectTracker.Start();
}
public static Transform ConfigTrackable(string trackedName, string trackComName, GameObject modelPre)
{
IEnumerable<TrackableBehaviour> tbs = TrackerManager.Instance.GetStateManager().GetTrackableBehaviours();
foreach (TrackableBehaviour tb in tbs)
{
Debug.Log(tb.TrackableName);
if (tb.TrackableName == trackedName)
{
//tb.GetComponent<ImageTargetBehaviour>().ImageTarget = dataSet.
//tb.transform.parent = recogParent;
tb.gameObject.name = trackedName;
Debug.Log(Type.GetType(trackComName));
//动态添加识别组件
tb.gameObject.AddComponent(Type.GetType(trackComName));
tb.gameObject.AddComponent<TurnOffBehaviour>();
GameObject augmentation = Instantiate(modelPre, tb.transform);
if (tb.CurrentStatus == TrackableBehaviour.Status.TRACKED)
augmentation.gameObject.SetActive(true);
else
augmentation.gameObject.SetActive(false);
return augmentation.transform;
}
}
return null;
}
/// <summary>
/// 关闭指定识别数据集
/// </summary>
/// <param name="datasetName">数据集名称或绝对路径</param>
public static void DeactivateDateset(string datasetName)
{
ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
IEnumerable<DataSet> activeDataSets = objectTracker.GetActiveDataSets();
List<DataSet> activeDataSetsToBeRemoved = activeDataSets.ToList();
List<DataSet> dataSetsToBeActive = new List<DataSet>();
foreach (DataSet ads in activeDataSetsToBeRemoved)
{
objectTracker.DeactivateDataSet(ads);
if (!ads.Path.Contains(datasetName))
{
dataSetsToBeActive.Add(ads);
}
else
{
objectTracker.DestroyDataSet(ads, false);
var trackables = ads.GetTrackables();
foreach (var item in trackables)
{
TrackerManager.Instance.GetStateManager().DestroyTrackableBehavioursForTrackable(item, true);
}
}
}
objectTracker.Stop();
foreach (DataSet ds in dataSetsToBeActive)
{
objectTracker.ActivateDataSet(ds);
}
objectTracker.Start();
}
/// <summary>
/// 设置同时识别Image的个数
/// </summary>
/// <param name="TrackedCount"></param>
public virtual void SetTrackedImageCount(int TrackedCount)
{
VuforiaUnity.SetHint(VuforiaUnity.VuforiaHint.HINT_MAX_SIMULTANEOUS_IMAGE_TARGETS, TrackedCount);
}
}