最新高通Vuforia 6-2-10版本 对应API 动态加载、卸载识别库 闪光灯 对焦 翻转摄像头 动态更改识别数量等

简介: 整理了下Vuforia 6-2-10 版本对应识别功能的函数TrackableSettings类主要功能开启扩展追踪获取当前激活识别库名称列表激活指定的识别数据集关闭指定识别数据集动态载入识别数据集卸载所...

整理了下Vuforia 6-2-10 版本对应识别功能的函数


  • TrackableSettings类主要功能

    • 开启扩展追踪
    • 获取当前激活识别库名称列表
    • 激活指定的识别数据集
    • 关闭指定识别数据集
    • 动态载入识别数据集
    • 卸载所有识别数据集(识别库)
    • 设置同时识别Image的个数
    • 设置同时识别3D物体的个数

代码块如下

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Vuforia;

/// <summary>
/// 追踪功能设置类
/// </summary>
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<TrackableSettings>();
                }
            }
            return instance;
        }
    }

    /// <summary>
    /// 识别数据集所在的文件路径
    /// </summary>
    private string dataSetDirectoryPath = null;

/// <summary>
/// 已经激活的识别数据集
/// </summary>
    protected Dictionary<string, string> mCurrentActiveDataSets = new Dictionary<string, string>();
    /// <summary>
    /// 所有数据集对应的路径
    /// </summary>
    protected Dictionary<string, string> mCurrentAllDataSets = new Dictionary<string, string>();
    /// <summary>
    /// 是否使用本地内置识别库
    /// </summary>
    public bool bLocal = false;
    /// <summary>
    /// 扩展追踪是否开启
    /// </summary>
    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);
        }
    }
    /// <summary>
    /// 获得扩展追踪开启状态
    /// </summary>
    /// <returns></returns>
    public bool IsExtendedTrackingEnabled()
    {
        return mExtTrackingEnabled;
    }

    /// <summary>
    /// 开启、关闭扩展追踪模式
    /// </summary>
    /// <param name="extTrackingEnabled">是否开启</param>
    public virtual void SwitchExtendedTracking(bool extTrackingEnabled)
    {
        //返回可以访问当前所有跟踪的状态管理器实例。
        StateManager stateManager = TrackerManager.Instance.GetStateManager();

        // 我们遍历所有trackablebehaviours启动或停止扩展跟踪他们所代表的目标。
        bool success = true;
        foreach (var tb in stateManager.GetTrackableBehaviours())
        {
            if (tb is ImageTargetBehaviour)
            {
                ImageTargetBehaviour itb = tb as ImageTargetBehaviour;
                if (extTrackingEnabled)
                {
                    if (!itb.ImageTarget.StartExtendedTracking())
                    {
                        success = false;
                        Debug.LogError("扩展跟踪开始失败,对应目标: " + itb.TrackableName);
                    }
                }
                else
                {
                    itb.ImageTarget.StopExtendedTracking();
                }
            }
            else if (tb is MultiTargetBehaviour)
            {
                MultiTargetBehaviour mtb = tb as MultiTargetBehaviour;
                if (extTrackingEnabled)
                {
                    if (!mtb.MultiTarget.StartExtendedTracking())
                    {
                        success = false;
                        Debug.LogError("Failed to start Extended Tracking on Target " + mtb.TrackableName);
                    }
                }
                else
                {
                    mtb.MultiTarget.StopExtendedTracking();
                }
            }
            else if (tb is CylinderTargetBehaviour)
            {
                CylinderTargetBehaviour ctb = tb as CylinderTargetBehaviour;
                if (extTrackingEnabled)
                {
                    if (!ctb.CylinderTarget.StartExtendedTracking())
                    {
                        success = false;
                        Debug.LogError("Failed to start Extended Tracking on Target " + ctb.TrackableName);
                    }
                }
                else
                {
                    ctb.CylinderTarget.StopExtendedTracking();
                }
            }
            else if (tb is ObjectTargetBehaviour)
            {
                ObjectTargetBehaviour otb = tb as ObjectTargetBehaviour;
                if (extTrackingEnabled)
                {
                    if (!otb.ObjectTarget.StartExtendedTracking())
                    {
                        success = false;
                        Debug.LogError("Failed to start Extended Tracking on Target " + otb.TrackableName);
                    }
                }
                else
                {
                    otb.ObjectTarget.StopExtendedTracking();
                }
            }
            else if (tb is VuMarkBehaviour)
            {
                VuMarkBehaviour vmb = tb as VuMarkBehaviour;
                if (extTrackingEnabled)
                {
                    if (!vmb.VuMarkTemplate.StartExtendedTracking())
                    {
                        success = false;
                        Debug.LogError("Failed to start Extended Tracking on Target " + vmb.TrackableName);
                    }
                }
                else
                {
                    vmb.VuMarkTemplate.StopExtendedTracking();
                }
            }
        }
        mExtTrackingEnabled = success && extTrackingEnabled;
    }
    /// <summary>
    /// 获取当前激活识别库名称列表
    /// </summary>
    /// <returns></returns>
    public List<string> GetActiveDatasetNameList()
    {
        ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        List<DataSet> activeDataSets = tracker.GetActiveDataSets().ToList();
        List<string> tempList = new List<string>();
        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;
    }
    /// <summary>
    /// 激活指定的识别数据集
    /// </summary>
    /// <param name="datasetName">数据集名称或绝对路径</param>
    public virtual void ActivateDataSet(string datasetName)
    {
        if (mCurrentActiveDataSets.ContainsKey(datasetName))
        {
            Debug.Log(string.Format("要激活的识别库:{0}已经加载", datasetName));
            return;
        } 

        // objecttracker跟踪包含在数据集,提供了用于创建和方法的激活数据。
        ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        IEnumerable<DataSet> datasets = objectTracker.GetDataSets();

        IEnumerable<DataSet> activeDataSets = objectTracker.GetActiveDataSets();
        List<DataSet> 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();
    }

    /// <summary>
    /// 关闭指定识别数据集
    /// </summary>
    /// <param name="datasetName">数据集名称或绝对路径</param>
    public virtual void DeactivateDateset(string datasetName)
    {
        if (!mCurrentActiveDataSets.ContainsKey(datasetName))
        {
            Debug.Log(string.Format("要关闭的识别库:{0}不存在", datasetName));
            return;
        }

        ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        IEnumerable<DataSet> datasets = objectTracker.GetDataSets();

        IEnumerable<DataSet> activeDataSets = objectTracker.GetActiveDataSets();

        List<DataSet> activeDataSetsToBeRemoved = activeDataSets.ToList();

        List<DataSet> dataSetsToBeActive = new List<DataSet>();

        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();
    }
    /// <summary>
    /// 载入识别数据集
    /// </summary>
    /// <param name="DataSetName">数据集名称</param>
    /// <param name="Local">是否是本地识别数据集</param>
    public virtual void LoadDataSet(string DataSetName, bool Local = true)
    {
        ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        objectTracker.Stop();
        bool bLoadDataSet = false;
        DataSet mDataSet = null;
        if (VuforiaRuntimeUtilities.IsVuforiaEnabled())
        {
            objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
            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();//开启识别器 (可以理解为摄像头)  
    }
    /// <summary>
    /// 更新ImageTarget上的信息
    /// </summary>
   public virtual void UpdateImageTarget()
    {
        ImageTargetBehaviour[] m_ImageTargetBehaviours = GameObject.FindObjectsOfType<ImageTargetBehaviour>();
        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<DefaultTrackableEventHandler>();
            imageTargetBehaviour.gameObject.AddComponent<TurnOffBehaviour>();
        }
    }
    /// <summary>
    /// 卸载所有识别数据集(识别库)
    /// </summary>
    public virtual void UnLoadAllDataSet()
    {
        ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        bool isVuforiaEnabled = VuforiaRuntimeUtilities.IsVuforiaEnabled();

        if (isVuforiaEnabled)
        {
            //1. 关闭跟踪器
            objectTracker.Stop();
            //获取对应数据集合集(和激活数据集写法略有不同)
            IEnumerable<DataSet> dataSets = objectTracker.GetActiveDataSets();
            IEnumerator<DataSet> dEnumerator = dataSets.GetEnumerator();
            List<DataSet> listDataSet = new List<DataSet>();

            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<ImageTargetBehaviour>();
            IEnumerable<TrackableBehaviour> 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("销毁数据失败");
        }
    }

    /// <summary>
    /// 设置同时识别Image的个数
    /// </summary>
    /// <param name="TrackedCount"></param>
    public virtual void SetTrackedImageCount(int TrackedCount)
    {
        VuforiaUnity.SetHint(VuforiaUnity.VuforiaHint.HINT_MAX_SIMULTANEOUS_IMAGE_TARGETS, TrackedCount);
    }
    /// <summary>
    /// 设置同时识别3D物体的个数
    /// </summary>
    /// <param name="TrackedCount"></param>
    public virtual void SetTrackedObjectCount(int TrackedCount)
    {
        VuforiaUnity.SetHint(VuforiaUnity.VuforiaHint.HINT_MAX_SIMULTANEOUS_OBJECT_TARGETS, TrackedCount);
    }
}


  • FrameRateSettings类主要功能

    • 设置对应帧率

代码块如下:

using UnityEngine;
using Vuforia;


/// <summary>
///帧率设置类
/// </summary>
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<FrameRateSettings>();
                }
            }
            return instance;
        }
    }

    void Awake()
    {
        //if (m_instance == null) m_instance = this;

        //if (gameObject.name == "GameObject") gameObject.name = Name;
    }


    void Start()
    {
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
    }


    /// <summary>
    /// 高通全面启动的帧率设置回调
    /// </summary>
    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;
/// <summary>
/// 摄像机功能设置类
/// </summary>
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<CameraSettings>();
                }
            }
            return instance;
        }

    }
    /// <summary>
    /// Vuforia是否完全启动
    /// </summary>
    private bool mVuforiaStarted = false;
    /// <summary>
    /// 自动对焦是否开启
    /// </summary>
    private bool mAutofocusEnabled = true;
    /// <summary>
    /// 闪光灯是否开启
    /// </summary>
    private bool mFlashTorchEnabled = false;
    /// <summary>
    /// 摄像头朝向
    /// </summary>
    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);
    }
    /// <summary>
    /// Vuforia完全启动回调
    /// </summary>
    private void OnVuforiaStarted()
    {
        mVuforiaStarted = true;
        //开启自动对焦
        SwitchAutofocus(true);
    }
    /// <summary>
    /// Vuforia停止和恢复的回调
    /// </summary>
    /// <param name="paused"></param>
    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;
        }
    }

    /// <summary>
    /// 闪光灯打开或或关闭状态
    /// </summary>
    /// <returns></returns>
    public bool IsFlashTorchEnabled()
    {
        return mFlashTorchEnabled;
    }
    /// <summary>
    /// 闪光灯开关
    /// </summary>
    /// <param name="ON">是否开启闪光灯</param>
    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;
    }
    /// <summary>
    /// 是否开启自动对焦
    /// </summary>
    /// <param name="ON">是否开启自动对焦</param>
    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);
        }
    }
    /// <summary>
    /// 强制触发一次对焦
    /// </summary>
    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);
        }
    }

    /// <summary>
    /// 前后摄像机切换
    /// </summary>
    /// <param name="camDir">摄像机方向</param>
    public virtual void SelectCamera(CameraDevice.CameraDirection camDir)
    {
        if (RestartCamera(camDir))
        {
            mActiveDirection = camDir;

            // Upon camera restart, flash is turned off
            mFlashTorchEnabled = false;
        }
    }
    /// <summary>
    /// 转换摄像机需要把原来的摄像机状态关掉卸载然后重新初始化
    /// </summary>
    /// <param name="direction">摄像机方向</param>
    /// <returns></returns>
    public virtual bool RestartCamera(CameraDevice.CameraDirection direction)
    {
        ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        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;
    }
    /// <summary>
    /// 在IOS上翻转摄像头会出现摄像头上下颠倒的情况,在CameraDevice.Instance.Start()后调用次函数,反之忽略即可
    /// </summary>
    /// <returns></returns>
    IEnumerator Refresh()
    {
        yield return new WaitForEndOfFrame();
        Camera.main.transform.localEulerAngles = new Vector3(0, 0, 180.0f);
    }
    /// <summary>
    /// 是否为前置摄像头
    /// </summary>
    /// <returns></returns>
    public virtual bool IsFrontCameraActive()
    {
        return (mActiveDirection == CameraDevice.CameraDirection.CAMERA_FRONT);
    }

}


  • InitErrorHandler类主要功能

    • Vuforia初始化时对应错误的回调(CustomLog自己封装的)

代码块如下:

using UnityEngine;
using System.Collections;
using Vuforia;
using Custom.Log;

public class InitErrorHandler : MonoBehaviour
{
    private string key;
    void Awake()
    {
        VuforiaRuntime.Instance.RegisterVuforiaInitErrorCallback(OnInitError);
        if (Debuger.EnableLog)
        {
            Debug.Log("+");
        }
        else
        {
            Debug.Log("-");
        }
    }
    private void OnInitError(VuforiaUnity.InitError error)
    {
        if (error != VuforiaUnity.InitError.INIT_SUCCESS)
        {
            ShowErrorMessage(error);
        }
    }
    private void CustomLog(string str)
    {
        this.Log(str);
    }

    private void ShowErrorMessage(VuforiaUnity.InitError errorCode)
    {
        switch (errorCode)
        {
            //外部设备检测失败错误(摄像头)
            case VuforiaUnity.InitError.INIT_EXTERNAL_DEVICE_NOT_DETECTED:
                {
                    string temp = "Vuforia初始化失败 " + "没有设备所需的外部硬件对接";
                    CustomLog(temp);
                    break;
                }
            // 没有输入密钥的错误
            case VuforiaUnity.InitError.INIT_LICENSE_ERROR_MISSING_KEY:
                {
                    string temp = "Vuforia 密钥没有找到";
                    CustomLog(temp);
                }
                break;
            // 输入的密钥无效的错误
            case VuforiaUnity.InitError.INIT_LICENSE_ERROR_INVALID_KEY:
                {
                    key = VuforiaConfiguration.Instance.Vuforia.LicenseKey;
                    if (key.Length > 10)
                    {
                        string temp = "Vuforia 密钥无效:请申请有效的密钥";
                        CustomLog(temp);
                    }
                    else
                    {
                        string temp = "Vuforia 密钥无效:密钥不完整";
                        CustomLog(temp);
                    }
                    break;
                }
            case VuforiaUnity.InitError.INIT_LICENSE_ERROR_NO_NETWORK_TRANSIENT:
                {
                    string temp =
   "无法接触服务器。请稍后再试";
                    CustomLog(temp);
                    break;
                }
            case VuforiaUnity.InitError.INIT_LICENSE_ERROR_NO_NETWORK_PERMANENT:
                {
                    string temp =
   "没有可用的网络。请确保你与互联网连接。";
                    CustomLog(temp);
                    break;
                }
            case VuforiaUnity.InitError.INIT_LICENSE_ERROR_CANCELED_KEY:
                {
                    key = VuforiaConfiguration.Instance.Vuforia.LicenseKey;
                    string temp = "这款应用程序的许可密钥已被取消,可能不再使用";
                    CustomLog(temp);
                    break;
                }
            case VuforiaUnity.InitError.INIT_LICENSE_ERROR_PRODUCT_TYPE_MISMATCH:
                {
                    key = VuforiaConfiguration.Instance.Vuforia.LicenseKey;
                    string temp = "输入的密钥与对应的产品不相符";
                    CustomLog(temp);
                }
                break;
//#if (UNITY_IPHONE || UNITY_IOS)
                case VuforiaUnity.InitError.INIT_NO_CAMERA_ACCESS:
                {
                    string temp =
                       "当在iOS设备上运行时,用户必须明确允许该应用访问摄像头。要恢复设备上的摄像头访问权限,请访问" +
                       "Settings > Privacy > Camera > [萌伴小学堂] 然后开启权限";
                    CustomLog(temp);
                    break;
                }
//#endif
            case VuforiaUnity.InitError.INIT_DEVICE_NOT_SUPPORTED:
                {
                    string temp =
   "Vuforia初始化失败:此设备不支持";
                    CustomLog(temp);
                    break;
                }

            case VuforiaUnity.InitError.INIT_ERROR:
                {
                    string temp = "Vuforia初始化失败:未知";
                    CustomLog(temp);
                    break;
                }
        }

        Debug.Log(errorCode);



    } //PRIVATE_METHODS

}

相关文章
|
1月前
|
存储 开发框架 .NET
.NET 8 实现无实体库表 API 部署服务
【10月更文挑战第12天】在.NET 8中,可通过以下步骤实现无实体库表的API部署:首先安装.NET 8 SDK及开发工具,并选用轻量级Web API框架如ASP.NET Core;接着创建新项目并设计API,利用内存数据结构模拟数据存储;最后配置项目设置并进行测试与部署。此方法适用于小型项目或临时解决方案,但对于大规模应用仍需考虑持久化存储以确保数据可靠性与可扩展性。
|
2月前
|
JSON 资源调度 JavaScript
Vue框架中Ajax请求的实现方式:使用axios库或fetch API
选择 `axios`还是 `fetch`取决于项目需求和个人偏好。`axios`提供了更丰富的API和更灵活的错误处理方式,适用于需要复杂请求配置的场景。而 `fetch`作为现代浏览器的原生API,使用起来更为简洁,但在旧浏览器兼容性和某些高级特性上可能略显不足。无论选择哪种方式,它们都能有效地在Vue应用中实现Ajax请求的功能。
41 4
|
4月前
|
人工智能 API
阿里云微服务引擎及 API 网关 2024 年 6 月产品动态
阿里云微服务引擎及 API 网关 2024 年 6 月产品动态
158 22
|
3月前
|
API 数据库 索引
indexedDB 操作库IDBWRAPPER 教程翻译及API翻译第二部分part2
indexedDB 操作库IDBWRAPPER 教程翻译及API翻译第二部分part2
|
3月前
|
文字识别 算法 API
视觉智能开放平台产品使用合集之海外是否可以访问人物动漫化的api版本
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
50 0
|
3月前
|
JSON API 网络架构
【Azure Developer】Azure REST API: 如何通过 API查看 Recovery Services Vaults(恢复保管库)的备份策略信息? 如备份中是否含有虚拟机的Disk
【Azure Developer】Azure REST API: 如何通过 API查看 Recovery Services Vaults(恢复保管库)的备份策略信息? 如备份中是否含有虚拟机的Disk
|
4月前
|
SQL 并行计算 API
Dask是一个用于并行计算的Python库,它提供了类似于Pandas和NumPy的API,但能够在大型数据集上进行并行计算。
Dask是一个用于并行计算的Python库,它提供了类似于Pandas和NumPy的API,但能够在大型数据集上进行并行计算。
|
3月前
|
Oracle Java 关系型数据库
JDK版本特性问题之在 JDK 11 中,HTTP Client API 的特点有哪些
JDK版本特性问题之在 JDK 11 中,HTTP Client API 的特点有哪些
|
3月前
|
缓存 JavaScript 前端开发
微信 JS-SDK Demo “分享信息设置” API 及数字签名生成方法(NodeJS版本)
微信 JS-SDK Demo “分享信息设置” API 及数字签名生成方法(NodeJS版本)更新时间(2020-10-29)
|
4月前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 API 网关 2024 年 06 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要

热门文章

最新文章