整理了下Vuforia 7 版本对应识别功能的函数 基本上和6版本无差异
Vuforia版本获取
string vuforiaVersion = Vuforia.VuforiaUnity.GetVuforiaLibraryVersion();
string unityVersion = UnityEngine.Application.unityVersion;
UnityEngine.Debug.Log("Vuforia " + vuforiaVersion + "\nUnity " + unityVersion);
控制vuforia摄像机对应的Texture渲染
VuforiaRenderer.Instance.Pause(true);//关闭渲染 参数false开始渲染
-
TrackableSettings类主要功能
-
开启扩展追踪
-
获取当前激活识别库名称列表
-
激活指定的识别数据集
-
关闭指定识别数据集
-
动态载入识别数据集
-
卸载所有识别数据集(识别库)
-
设置同时识别Image的个数
-
设置同时识别3D物体的个数
-
代码块如下
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Vuforia;
///
/// 追踪功能设置类
///
public class TrackableSettings : MonoBehaviour
{
public const string Name = "TrackableSettings";
static TrackableSettings() { }
protected TrackableSettings() { }
protected static volatile TrackableSettings instance = null;
protected readonly object msyncRoot = new object();
protected static readonly object mstaticSyncRoot = new object();
public static TrackableSettings Instance
{
get
{
if (instance == null)
{
lock (mstaticSyncRoot)
{
if (instance == null) instance = new GameObject(Name).AddComponent();
}
}
return instance;
}
}
///
/// 识别数据集所在的文件路径
///
private string dataSetDirectoryPath = null;
///
/// 已经激活的识别数据集
///
protected Dictionary mCurrentActiveDataSets = new Dictionary();
///
/// 所有数据集对应的路径
///
protected Dictionary mCurrentAllDataSets = new Dictionary();
///
/// 是否使用本地内置识别库
///
public bool bLocal = false;
///
/// 扩展追踪是否开启
///
protected bool mExtTrackingEnabled = false;
private void Awake()
{
//if (m_instance == null) m_instance = this;
//if (gameObject.name == "GameObject") gameObject.name = Name;
}
private void Start()
{
Initialize();
}
private void Initialize()
{
dataSetDirectoryPath = Application.persistentDataPath + "/" + "DataDirectory" + "/";
if (!Directory.Exists(dataSetDirectoryPath))
{
Directory.CreateDirectory(dataSetDirectoryPath);
}
}
///
/// 获得扩展追踪开启状态
///
///
public bool IsExtendedTrackingEnabled()
{
return mExtTrackingEnabled;
}
///
/// 开启、关闭扩展追踪模式
///
/// 是否开启
public virtual void SwitchExtendedTracking(bool enableExtendedTracking)
{
//返回可以访问当前所有跟踪的状态管理器实例。
StateManager stateManager = TrackerManager.Instance.GetStateManager();
// 我们遍历所有trackablebehaviours启动或停止扩展跟踪他们所代表的目标。
bool success = true;
foreach (var tb in stateManager.GetTrackableBehaviours())
{
if (tb is DataSetTrackableBehaviour)
{
if (tb.Trackable is ObjectTarget)
{
ObjectTarget target = tb.Trackable as ObjectTarget;
if (enableExtendedTracking)
{
success = target.StartExtendedTracking();
if (success)
Debug.Log("Successfully started Extended Tracking on Target: " + tb.TrackableName);
else
Debug.LogError("Failed to start Extended Tracking on Target: " + tb.TrackableName);
}
else
{
target.StopExtendedTracking();
}
}
}
}
mExtTrackingEnabled = success && enableExtendedTracking;
}
///
/// 获取当前激活识别库名称列表
///
///
public List GetActiveDatasetNameList()
{
ObjectTracker tracker = TrackerManager.Instance.GetTracker();
List activeDataSets = tracker.GetActiveDataSets().ToList();
List tempList = new List();
for (int i = 0; i < activeDataSets.Count; i++)
{
string datasetPath = activeDataSets.ElementAt(i).Path;
Debug.Log("datasetPath:" + datasetPath);
string datasetName = datasetPath.Substring(datasetPath.LastIndexOf("/") + 1);
Debug.Log("datasetName:" + datasetName);
tempList.Add(datasetName.TrimEnd(".xml".ToCharArray()));
}
return tempList;
}
///
/// 激活指定的识别数据集
///
/// 数据集名称或绝对路径
public virtual void ActivateDataSet(string datasetName)
{
if (mCurrentActiveDataSets.ContainsKey(datasetName))
{
Debug.Log(string.Format("要激活的识别库:{0}已经加载", datasetName));
return;
}
// objecttracker跟踪包含在数据集,提供了用于创建和方法的激活数据。
ObjectTracker objectTracker = TrackerManager.Instance.GetTracker();
IEnumerable datasets = objectTracker.GetDataSets();
IEnumerable activeDataSets = objectTracker.GetActiveDataSets();
List activeDataSetsToBeRemoved = activeDataSets.ToList();
// 1. 循环遍历所有的活动数据集并禁用它们
foreach (DataSet ads in activeDataSetsToBeRemoved)
{
objectTracker.DeactivateDataSet(ads);
}
// 在ObjectTracker运行时,不应该对数据集进行交换
// 2. 所以首先要关闭tracker
objectTracker.Stop();
// 3. 然后,查找新数据集,如果存在,激活它
foreach (DataSet ds in datasets)
{
if (ds.Path.Contains(datasetName))
{
objectTracker.ActivateDataSet(ds);
mCurrentActiveDataSets.Add(datasetName, ds.Path);
}
}
// 4. 最后重启traker.
objectTracker.Start();
}
///
/// 关闭指定识别数据集
///
/// 数据集名称或绝对路径
public virtual void DeactivateDateset(string datasetName)
{
if (!mCurrentActiveDataSets.ContainsKey(datasetName))
{
Debug.Log(string.Format("要关闭的识别库:{0}不存在", datasetName));
return;
}
ObjectTracker objectTracker = TrackerManager.Instance.GetTracker();
IEnumerable datasets = objectTracker.GetDataSets();
IEnumerable activeDataSets = objectTracker.GetActiveDataSets();
List activeDataSetsToBeRemoved = activeDataSets.ToList();
List dataSetsToBeActive = new List();
foreach (DataSet ads in activeDataSetsToBeRemoved)
{
if (!ads.Path.Contains(datasetName))
{
dataSetsToBeActive.Add(ads);
}
objectTracker.DeactivateDataSet(ads);
}
objectTracker.Stop();
foreach (DataSet ds in dataSetsToBeActive)
{
objectTracker.ActivateDataSet(ds);
}
mCurrentActiveDataSets.Remove(datasetName);
objectTracker.Start();
}
///
/// 载入识别数据集
///
/// 数据集名称
/// 是否是本地识别数据集
public virtual void LoadDataSet(string DataSetName, bool Local = true)
{
ObjectTracker objectTracker = TrackerManager.Instance.GetTracker();
objectTracker.Stop();
bool bLoadDataSet = false;
DataSet mDataSet = null;
if (VuforiaRuntimeUtilities.IsVuforiaEnabled())
{
objectTracker = TrackerManager.Instance.GetTracker();
mDataSet = objectTracker.CreateDataSet();
if (!Local)
{
bLoadDataSet = mDataSet.Load(dataSetDirectoryPath + DataSetName + ".xml", VuforiaUnity.StorageType.STORAGE_ABSOLUTE);//绝对路径 一般用来加载网络下载的识别库(dat和xml文件)
}
else
{
bLoadDataSet = mDataSet.Load(DataSetName);//本地预制的识别库
}
}
if (bLoadDataSet)
{
mCurrentAllDataSets.Add(DataSetName, mDataSet.Path);
UpdateImageTarget();
Debug.Log("加载识别数据库成功并激活");
if (objectTracker.ActivateDataSet(mDataSet))
{
mCurrentActiveDataSets.Add(DataSetName, mDataSet.Path);
}
else
{
Debug.Log(DataSetName + "激活失败");
}
}
else
{
Debug.Log("加载识别数据库失败");
}
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO);
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
objectTracker.Start();//开启识别器 (可以理解为摄像头)
}
///
/// 更新ImageTarget上的信息
///
public virtual void UpdateImageTarget()
{
ImageTargetBehaviour[] m_ImageTargetBehaviours = GameObject.FindObjectsOfType();
for (int i = 0; i < m_ImageTargetBehaviours.Length; i++)
{
ImageTargetBehaviour imageTargetBehaviour = m_ImageTargetBehaviours[i];
imageTargetBehaviour.name = m_ImageTargetBehaviours[i].ImageTarget.Name + "Target";
imageTargetBehaviour.gameObject.AddComponent();
imageTargetBehaviour.gameObject.AddComponent();
}
}
///
/// 卸载所有识别数据集(识别库)
///
public virtual void UnLoadAllDataSet()
{
ObjectTracker objectTracker = TrackerManager.Instance.GetTracker();
bool isVuforiaEnabled = VuforiaRuntimeUtilities.IsVuforiaEnabled();
if (isVuforiaEnabled)
{
//1. 关闭跟踪器
objectTracker.Stop();
//获取对应数据集合集(和激活数据集写法略有不同)
IEnumerable dataSets = objectTracker.GetActiveDataSets();
IEnumerator dEnumerator = dataSets.GetEnumerator();
List listDataSet = new List();
while (dEnumerator.MoveNext())
{
listDataSet.Add(dEnumerator.Current);
}
//关闭每一个数据集
for (int i = 0; i < listDataSet.Count; i++)
{
Debug.Log("关闭对应数据集:" + listDataSet[i].Path);
objectTracker.DeactivateDataSet(listDataSet[i]);
}
for (int i = 0; i < listDataSet.Count; i++)
{
Debug.Log("关闭对应数据集:" + listDataSet[i].Path);
objectTracker.DestroyDataSet(listDataSet[i], false);
}
//管理所有可跟踪行为的状态
StateManager stateManager = TrackerManager.Instance.GetStateManager();
//ImageTargetBehaviour[] ImageTargetBehaviours = GameObject.FindObjectsOfType();
IEnumerable IETrackableBehaviours = stateManager.GetTrackableBehaviours();
//销毁对应创建的ImageTarget跟踪具体行为(就是在imagetarget上设置的参数)和销毁对应的GameObject
foreach (var tb in IETrackableBehaviours)
{
stateManager.DestroyTrackableBehavioursForTrackable(tb.Trackable, true);
}
objectTracker.DestroyAllDataSets(true);
Debug.Log("销毁识别数据成功");
mCurrentActiveDataSets.Clear();
mCurrentAllDataSets.Clear();
}
else
{
Debug.Log("销毁数据失败");
}
}
///
/// 设置同时识别Image的个数
///
///
public virtual void SetTrackedImageCount(int TrackedCount)
{
VuforiaUnity.SetHint(VuforiaUnity.VuforiaHint.HINT_MAX_SIMULTANEOUS_IMAGE_TARGETS, TrackedCount);
}
///
/// 设置同时识别3D物体的个数
///
///
public virtual void SetTrackedObjectCount(int TrackedCount)
{
VuforiaUnity.SetHint(VuforiaUnity.VuforiaHint.HINT_MAX_SIMULTANEOUS_OBJECT_TARGETS, TrackedCount);
}
}
-
FrameRateSettings类主要功能
-
设置对应帧率
-
代码块如下:
using UnityEngine;
using Vuforia;
///
///帧率设置类
///
public class FrameRateSettings : MonoBehaviour
{
public const string Name = "FrameRateSettings";
static FrameRateSettings() { }
protected FrameRateSettings() { }
protected static volatile FrameRateSettings instance = null;
protected readonly object msyncRoot = new object();
protected static readonly object mstaticSyncRoot = new object();
public static FrameRateSettings Instance
{
get
{
if (instance == null)
{
lock (mstaticSyncRoot)
{
if (instance == null) instance = new GameObject(Name).AddComponent();
}
}
return instance;
}
}
void Awake()
{
//if (m_instance == null) m_instance = this;
//if (gameObject.name == "GameObject") gameObject.name = Name;
}
void Start()
{
VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
}
///
/// 高通全面启动的帧率设置回调
///
private void OnVuforiaStarted()
{
//查询在unity中推荐的帧率(默认 AR或VR 省电 高帧数 四种模式)
int targetFps = VuforiaRenderer.Instance.GetRecommendedFps(VuforiaRenderer.FpsHint.NONE);
/*默认情况下,我们使用application.targetframerate设置推荐的帧速率。
谷歌纸板不使用垂直同步,和OVR明确禁用它。如果开发商
使用VSYNC的质量设置,他们也应该把他们的qualitysettings.vsynccount
根据上面返回的值。
例如:如果物体的FPS > 50 > vsynccount = 1;其他vsynccount = 2;
*/
if (Application.targetFrameRate != targetFps)
{
Debug.Log("Setting frame rate to " + targetFps + "fps");
Application.targetFrameRate = targetFps;
}
}
}
-
CameraSettings类主要功能
-
开启闪光灯
-
开启自动对焦
-
前后摄像机切换
-
代码块如下:
using UnityEngine;
using System.Collections;
using Vuforia;
///
/// 摄像机功能设置类
///
public class CameraSettings : MonoBehaviour
{
public const string Name = "CameraSettings";
static CameraSettings() { }
protected CameraSettings() { }
protected static volatile CameraSettings instance = null;
protected readonly object msyncRoot = new object();
protected static readonly object mstaticSyncRoot = new object();
public static CameraSettings Instance
{
get
{
if (instance == null)
{
lock (mstaticSyncRoot)
{
if (instance == null) instance = new GameObject(Name).AddComponent();
}
}
return instance;
}
}
///
/// Vuforia是否完全启动
///
private bool mVuforiaStarted = false;
///
/// 自动对焦是否开启
///
private bool mAutofocusEnabled = true;
///
/// 闪光灯是否开启
///
private bool mFlashTorchEnabled = false;
///
/// 摄像头朝向
///
private CameraDevice.CameraDirection mActiveDirection = CameraDevice.CameraDirection.CAMERA_DEFAULT;
void Awake()
{
//if (m_instance == null) m_instance = this;
//if (gameObject.name == "GameObject") gameObject.name = Name;
}
void Start()
{
VuforiaARController vuforia = VuforiaARController.Instance;
//vuforia已经全面启动回调,包括相机和跟踪器。可以注册多个回调函数
vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted);
//停止和恢复的回调,这个是回复后重新让摄像机对焦(对应状态自动传入对应的bool值)
vuforia.RegisterOnPauseCallback(OnPaused);
}
///
/// Vuforia完全启动回调
///
private void OnVuforiaStarted()
{
mVuforiaStarted = true;
//开启自动对焦
SwitchAutofocus(true);
}
///
/// Vuforia停止和恢复的回调
///
///
private void OnPaused(bool paused)
{
bool appResumed = !paused;
if (appResumed && mVuforiaStarted)
{
// 恢复原来的对焦方式
if (mAutofocusEnabled)
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
else
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
else
{
//闪光灯状态更改 因为闪光灯会自动被操作系统关闭
mFlashTorchEnabled = false;
}
}
///
/// 闪光灯打开或或关闭状态
///
///
public bool IsFlashTorchEnabled()
{
return mFlashTorchEnabled;
}
///
/// 闪光灯开关
///
/// 是否开启闪光灯
public virtual void SwitchFlashTorch(bool ON)
{
if (CameraDevice.Instance.SetFlashTorchMode(ON))
{
Debug.Log("成功开启闪光灯 " + ON);
mFlashTorchEnabled = ON;
}
else
{
Debug.Log("开启闪光灯失败 " + ON);
mFlashTorchEnabled = false;
}
}
public virtual bool IsAutofocusEnabled()
{
return mAutofocusEnabled;
}
///
/// 是否开启自动对焦
///
/// 是否开启自动对焦
public virtual void SwitchAutofocus(bool ON)
{
if (ON)
{
if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO))
{
Debug.Log("成功开启连续对焦");
mAutofocusEnabled = true;
}
else
{
// Fallback to normal focus mode
Debug.Log("未能启用连续自动对焦,切换到正常对焦模式");
mAutofocusEnabled = false;
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
}
else
{
Debug.Log("禁用连续自动对焦(启用正常对焦模式).");
mAutofocusEnabled = false;
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
}
///
/// 强制触发一次对焦
///
public virtual void TriggerAutofocusEvent()
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO);
// 恢复到原来的对焦模式
StartCoroutine(RestoreOriginalFocusMode());
}
private IEnumerator RestoreOriginalFocusMode()
{
yield return new WaitForSeconds(1.5f);
if (mAutofocusEnabled)
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}
else
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
}
///
/// 前后摄像机切换
///
/// 摄像机方向
public virtual void SelectCamera(CameraDevice.CameraDirection camDir)
{
if (RestartCamera(camDir))
{
mActiveDirection = camDir;
// Upon camera restart, flash is turned off
mFlashTorchEnabled = false;
}
}
///
/// 转换摄像机需要把原来的摄像机状态关掉卸载然后重新初始化
///
/// 摄像机方向
///
public virtual bool RestartCamera(CameraDevice.CameraDirection direction)
{
ObjectTracker tracker = TrackerManager.Instance.GetTracker();
if (tracker != null)
{
tracker.Stop();
}
CameraDevice.Instance.Stop();
CameraDevice.Instance.Deinit();
if (!CameraDevice.Instance.Init(direction))
{
Debug.Log("初始化失败,指定的方向为: " + direction.ToString());
return false;
}
if (!CameraDevice.Instance.Start())
{
Debug.Log("摄像机启动失败,指定的方向为: " + direction.ToString());
return false;
}
if (tracker != null)
{
if (!tracker.Start())
{
Debug.Log("Tracker重新启动失败");
return false;
}
}
return true;
}
///
/// 在IOS上翻转摄像头会出现摄像头上下颠倒的情况,在CameraDevice.Instance.Start()后调用次函数,反之忽略即可
///
///
IEnumerator Refresh()
{
yield return new WaitForEndOfFrame();
Camera.main.transform.localEulerAngles = new Vector3(0, 0, 180.0f);
}
///
/// 是否为前置摄像头
///
///
public virtual bool IsFrontCameraActive()
{
return (mActiveDirection == CameraDevice.CameraDirection.CAMERA_FRONT);
}
}
-
InitErrorHandlers类主要功能
-
Vuforia初始化时对应错误的回调(CustomLog自己封装的)
-
代码块如下:
using UnityEngine;
using System.Collections;
using Vuforia;
public class InitErrorHandlers : MonoBehaviour
{
private string key;
void Awake()
{
VuforiaRuntime.Instance.RegisterVuforiaInitErrorCallback(OnInitError);
}
private void OnInitError(VuforiaUnity.InitError error)
{
if (error != VuforiaUnity.InitError.INIT_SUCCESS)
{
ShowErrorMessage(error);
}
}
private void ShowErrorMessage(VuforiaUnity.InitError errorCode)
{
switch (errorCode)
{
//外部设备检测失败错误(摄像头)
case VuforiaUnity.InitError.INIT_EXTERNAL_DEVICE_NOT_DETECTED:
{
string temp = "Vuforia初始化失败 " + "没有设备所需的外部硬件对接";
Debug.Log(""+temp+" ");
break;
}
// 没有输入密钥的错误
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_MISSING_KEY:
{
string temp = "Vuforia 密钥没有找到";
Debug.Log("" + temp + " ");
}
break;
// 输入的密钥无效的错误
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_INVALID_KEY:
{
key = VuforiaConfiguration.Instance.Vuforia.LicenseKey;
if (key.Length > 10)
{
string temp = "Vuforia 密钥无效:请申请有效的密钥";
Debug.Log("" + temp + " ");
}
else
{
string temp = "Vuforia 密钥无效:密钥不完整";
Debug.Log("" + temp + " ");
}
break;
}
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_NO_NETWORK_TRANSIENT:
{
string temp ="无法接触服务器。请稍后再试";
Debug.Log("" + temp + " ");
break;
}
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_NO_NETWORK_PERMANENT:
{
string temp = "没有可用的网络。请确保你与互联网连接。";
Debug.Log("" + temp + " ");
break;
}
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_CANCELED_KEY:
{
key = VuforiaConfiguration.Instance.Vuforia.LicenseKey;
string temp = "这款应用程序的许可密钥已被取消,可能不再使用";
Debug.Log("" + temp + " ");
break;
}
case VuforiaUnity.InitError.INIT_LICENSE_ERROR_PRODUCT_TYPE_MISMATCH:
{
key = VuforiaConfiguration.Instance.Vuforia.LicenseKey;
string temp = "输入的密钥与对应的产品不相符";
Debug.Log("" + temp + " ");
}
break;
//#if (UNITY_IPHONE || UNITY_IOS)
case VuforiaUnity.InitError.INIT_NO_CAMERA_ACCESS:
{
string temp =
"当在iOS设备上运行时,用户必须明确允许该应用访问摄像头。要恢复设备上的摄像头访问权限,请访问" +
"Settings > Privacy > Camera > [萌伴小学堂] 然后开启权限";
Debug.Log("" + temp + " ");
break;
}
//#endif
case VuforiaUnity.InitError.INIT_DEVICE_NOT_SUPPORTED:
{
string temp ="Vuforia初始化失败:此设备不支持";
Debug.Log("" + temp + " ");
break;
}
case VuforiaUnity.InitError.INIT_ERROR:
{
string temp = "Vuforia初始化失败:未知";
Debug.Log("" + temp + " ");
break;
}
}
Debug.Log(errorCode);
} //PRIVATE_METHODS
}