Unity Vuforia 动态生成识别图,返回识别图的监听

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

/*
 *  1.下载 XML Dat 文件到本地 (XML Dat 的路径和文件名要一样)
 *  2.根据本地XML路径动态生成Vuforial识别图
 *  3.生成完毕后 会返回识别图的监听 IEnumerable<ObserverBehaviour>
 *  4.绑定监听 ObserverBehaviour
 */
public class VufroiaTrigger
{
    public JsonData msg;
    public UserSceneItem usi;
    public string xmlFile;
    public string datFile;
    public static string LoaclxmlFile;
    public static string LoacldatFile;

    bool isData;
        bool isXML;
    public void init()
    {
        xmlFile = msg["vuforiaXML"].ToString();
        datFile = msg["vuforiaDat"].ToString();
        LoaclxmlFile = Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.xml";
        LoacldatFile = Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.dat";

        Debug.Log(" DGJ ===> " + LoaclxmlFile);
        Debug.Log(" DGJ ===>  " + LoacldatFile);
        LoadManager.Instance.loadVuforia(xmlFile, datFile,(bool b)=> {

            isXML = true;
            if(isData)
            {
              GameScene.Instance.StartCoroutine(loadVuforia());
            }

        }, (bool b) => {
            isData = true;
            if (isXML)
            {
                GameScene.Instance.StartCoroutine(loadVuforia());
            }
        });
    }


    IEnumerator loadVuforia()
    {
        Debug.Log("DGJ ===> 加载Database  " + LoaclxmlFile);
        IEnumerable<ObserverBehaviour> observer = null;
      
        observer = VuforiaBehaviour.Instance.ObserverFactory.CreateBehavioursFromDatabase(LoaclxmlFile);
        try
        {
        
         

        }
        catch (System.Exception e)
        {
           
          
        }
        yield return observer;
        // Create an Image Target from the database.

        if(observer !=null)
        {

            foreach (string item2 in usi.SpotsList.Keys)
            {
                foreach (var item111 in usi.SpotsList[item2].modelList.Keys)
                {
                    for (int ij = 0; ij < usi.SpotsList[item2].modelList[item111].materialList.Count; ij++)
                    {
                        Debug.Log("測試===2=》" + usi.SpotsList[item2].modelList[item111].materialList[ij].name);
                        Debug.Log("測試===2=》" + usi.SpotsList[item2].id + "_" + DataManager.Instance.ProjectID.ToString() + "_" + usi.id);
                        Debug.Log("測試===2=》" + usi.SpotsList[item2].triggerImageMark);
                    }
                }
            }
            foreach (ObserverBehaviour item in observer)
            {
              
                foreach (string item2 in usi.SpotsList.Keys)
                {
                    Debug.Log("HJJVufroia  正在匹配" + item.TargetName+"_"+ usi.SpotsList[item2].triggerImageMark);
                    if (item.TargetName == usi.SpotsList[item2].triggerImageMark)
                    {
                        Debug.Log("HJJVufroia  场景匹配成功"+ item.TargetName);
                        usi.SpotsList[item2].setObVuforia(item);
                    }
                }
               
                Debug.Log("DGJ ===> " + item.gameObject.name);
                GameManager.Instance.listVuforia.Add(item.gameObject);
            }

        }

       

        UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
        UserSceneUI.Instance.obj.GetComponent<UserChooseScene>().st.SetActive(true);
    }


    IEnumerator loadVuforia2()
    {
        Debug.Log("DGJ ===> 加载Database  " + LoaclxmlFile);
        IEnumerable<ObserverBehaviour> observer = null;
        // 加载识别图
        observer = VuforiaBehaviour.Instance.ObserverFactory.CreateBehavioursFromDatabase(LoaclxmlFile);
        try
        {



        }
        catch (System.Exception e)
        {


        }
        yield return observer;
        // Create an Image Target from the database.

        if (observer != null)
        {

         
            foreach (ObserverBehaviour item in observer)
            {

                foreach (string item2 in usi.SpotsList.Keys)
                {
                    Debug.Log("HJJVufroia  正在匹配" + item.TargetName + "_" + usi.SpotsList[item2].triggerImageMark);
                    if (item.TargetName == usi.SpotsList[item2].triggerImageMark)
                    {
                        Debug.Log("HJJVufroia  场景匹配成功" + item.TargetName);
                        usi.SpotsList[item2].setObVuforia(item);
                        /* setObVuforia 大致方法
                        this.obVuforia = obVuforia;
                        Debug.Log("添加Vuforia监听" + obVuforia.TargetName + "    景点===》" + name);

                        this.obVuforia.OnTargetStatusChanged += OnTargetStatusChanged;

                        obVuforia.gameObject.name = "ImageTarget_" + obVuforia.TargetName;
                        obVuforia.gameObject.AddComponent<TurnOffBehaviour>();
                        if (obVuforia.gameObject.GetComponent<DefaultObserverEventHandler>())
                        {
                            GameObject.Destroy(obVuforia.gameObject.GetComponent<DefaultObserverEventHandler>());
                        }
                        obVuforia.gameObject.AddComponent<DefaultObserverEventHandler>();
                        DefaultObserverEventHandler imageaction = obVuforia.gameObject.GetComponent<DefaultObserverEventHandler>();
                        obj = new GameObject("Cube");
                        obj.transform.parent = imageaction.transform;
                        obj.transform.localEulerAngles = new Vector3(90, 0, 0);
                        obj.transform.localPosition = Vector3.zero;
                        */
                    }
                }

                Debug.Log("DGJ ===> " + item.gameObject.name);
                GameManager.Instance.listVuforia.Add(item.gameObject);
            }

        }



        UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
        UserSceneUI.Instance.obj.GetComponent<UserChooseScene>().st.SetActive(true);
    }
}

  在相机上可以动态添加 

OpenXRCamera.Instance.head.gameObject.AddComponent<VuforiaBehaviour>();
OpenXRCamera.Instance.head.gameObject.AddComponent<DefaultInitializationErrorHandler>();

目前的缺点:

二次加载识别图的时候,已经加载过的识别图监听会返回报错,需要自己维护一个表 做判断, 如果有重复的话不要删除当前的监听物体,卸载掉原监听事件,新增新的监听事件(目前还是理论,没有实际测过)

 

posted @ 2024-01-04 18:51  D个人笔记  阅读(16)  评论(0编辑  收藏  举报