动态加载Vuforia识别信息

Posted GamePal

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态加载Vuforia识别信息相关的知识,希望对你有一定的参考价值。

Unity2017 ; vuforia-unity-6-2-10;

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Vuforia;
/// <summary>
/// 识别图管理器
/// </summary>
public class ImageTargetManager : MonoBehaviour
{
    /// <summary>
    /// 识别图加载完成
    /// </summary>
    public event Action<string[]> ImageTargerLoadedEvent;

    /// <summary>
    /// 本地文件路径
    /// </summary>
    private string m_localFilePath;
    /// <summary>
    /// 是否加载过
    /// </summary>
    private bool m_isLoaded;
    /// <summary>
    /// 数据集
    /// </summary>
    private DataSet m_dataSet;
    /// <summary>
    /// 识别器
    /// </summary>
    private ObjectTracker m_tracker;
    /// <summary>
    /// 识别图集合对象
    /// </summary>
    private ImageTargetBehaviour[] m_imageTargetBehaviours;

    private void Start()
    {
        m_isLoaded = false;
        //TODO  识别图相关信息加载
        //VuforiaBehaviour.Instance.RegisterVuforiaInitializedCallback(VuforiaInitedCallBack);
        VuforiaARController.Instance.RegisterVuforiaInitializedCallback(VuforiaInitedCallBack);
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(VuforiaStaredtCallBack);
    }
    /// <summary>
    /// 高通初始化完毕回调
    /// </summary>
    void VuforiaInitedCallBack()
    {
        m_localFilePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".xml";

        if (File.Exists(m_localFilePath))
        {
            //Load Local
            StartCoroutine(LoadLocalFile());
        }
        else
        {
            //Load NetWork
            //StartCoroutine(LoadNetworkFile());
        }
    }

    void VuforiaStaredtCallBack()
    {
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);  //auto focus
    }


    /// <summary>
    /// 加载网络配置文件
    /// </summary>
    /// <returns></returns>
    private IEnumerator LoadNetworkFile()
    {
        WWW wwwdat = new WWW(AppConfigConst.IMAGE_TARGET_FILE_PATH + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".dat");
        yield return wwwdat;

        Debug.Log(wwwdat.url);
        WWW wwwxml = new WWW(AppConfigConst.IMAGE_TARGET_FILE_PATH + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".xml");
        yield return wwwxml;

        File.WriteAllBytes(Application.persistentDataPath + "/" + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".dat", wwwdat.bytes);
        File.WriteAllBytes(Application.persistentDataPath + "/" + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".xml", wwwxml.bytes);
        StartCoroutine(LoadLocalFile());
    }


    /// <summary>
    /// 加载本地配置文件
    /// </summary>
    /// <returns></returns>
    IEnumerator LoadLocalFile()
    {
        bool isVuforiaEnabled = VuforiaRuntimeUtilities.IsVuforiaEnabled();
        if (isVuforiaEnabled && m_isLoaded == false)
        {
            if (m_dataSet == null)
            {
                m_tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
                m_dataSet = m_tracker.CreateDataSet();
            }
            if (m_dataSet.Load(m_localFilePath, VuforiaUnity.StorageType.STORAGE_ABSOLUTE))
            {
                m_isLoaded = true;
                m_tracker.ActivateDataSet(m_dataSet);
                UpdateImageTarget();
            }
            else
            {
                m_isLoaded = false;
            }
        }
        WWW www = new WWW("file:///" + m_localFilePath);
        yield return www;
    }


    /// <summary>
    /// 修改Imagetarget 的名称
    /// </summary>
    void UpdateImageTarget()
    {
        List<string> imagetargetNameList = new List<string>();
        m_imageTargetBehaviours = FindObjectsOfType<ImageTargetBehaviour>();
        for (int i = 0; i < m_imageTargetBehaviours.Length; i++)
        {
            ImageTargetBehaviour imageTargetBehaviour = m_imageTargetBehaviours[i];
            imageTargetBehaviour.name = "ImageTarget_" + imageTargetBehaviour.ImageTarget.Name;

            //imageTargetBehaviour.gameObject.AddComponent<DefaultTrackableEventHandler>();

            //读取 DefaultTrackableEventHandler.lua
            //imageTargetBehaviour.gameObject.AddComponent<DefaultTrackableEventHandler>();
            imageTargetBehaviour.gameObject.AddComponent<LuaTrackableEventHandler>();
            imageTargetBehaviour.gameObject.AddComponent<TurnOffBehaviour>();
            imagetargetNameList.Add(imageTargetBehaviour.name);
        }
        OnImageTargerLoadedEvent(imagetargetNameList.ToArray());
    }

    /// <summary>
    /// 获取所有的识别图对象
    /// </summary>
    /// <returns></returns>
    public ImageTargetBehaviour[] GetImageTargetBehaviours()
    {
        return m_imageTargetBehaviours;
    }

    protected virtual void OnImageTargerLoadedEvent(string[] obj)
    {
        var handler = ImageTargerLoadedEvent;
        if (handler != null)
        {
            handler(obj);
        }
    }
}


class AppConfigConst
{
    public static string IMAGE_TARGET_FILE_PATH = "";   //http url http://54.193.6.32:8080/U3dFileToServer/upLoad/WFJ/AssetBundle/
    public static string IMAGE_TARGET_FILE_NAME = "USTC";
}

 

以上是关于动态加载Vuforia识别信息的主要内容,如果未能解决你的问题,请参考以下文章

ARKit 和 Vuforia - 标记识别

Hololens Vuforia 物体识别

Unity-Hololens-Vuforia开发(图片识别和模型识别)

高通Vuforia(Unity3D)云识别初级使用教程

vuforia unity 识别图片出模型

Unity Vuforia 学习笔记