using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using Vuforia;
using QFramework;
///
/// 识别图管理器
///
public class ImageTargetManager : MonoSingleton
{
//public Text logtest;
//public Text errortext;
// public List listShowObj;
///
/// 识别图加载完成
///
public event Action ImageTargerLoadedEvent;
///
/// 本地文件路径
///
private string m_localFilePath;
///
/// 是否加载过
///
private bool m_isLoaded;
///
/// 数据集
///
private DataSet m_dataSet = null;
///
/// 识别器
///
private ObjectTracker m_tracker;
///
/// 识别图集合对象
///
private ImageTargetBehaviour[] m_imageTargetBehaviours;
private void Start()
{
// Debug.Log(" Inited " + m_isLoaded);
m_isLoaded = false;
//TODO 识别图相关信息加载
//VuforiaBehaviour.Instance.RegisterVuforiaInitializedCallback(VuforiaInitedCallBack);
try
{
VuforiaARController.Instance.RegisterVuforiaInitializedCallback(VuforiaInitedCallBack);
VuforiaARController.Instance.RegisterVuforiaStartedCallback(VuforiaStaredtCallBack);
}
catch (Exception e)
{
// errortext.text = e.Message;
}
}
///
/// 高通初始化完毕回调
///
void VuforiaInitedCallBack()
{
m_localFilePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".xml";
Debug.Log( " Inited@@@ "+m_localFilePath+" " +File.Exists(m_localFilePath));
//logtest.text = " Inited@@@ " + m_localFilePath + " " + File.Exists(m_localFilePath);
if (File.Exists(m_localFilePath))
{
//Load Local
StartCoroutine(LoadLocalFile());
}
else
{
//Load NetWork
//StartCoroutine(LoadNetworkFile());
}
}
void VuforiaStaredtCallBack()
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
// errortext.text = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO).ToString(); //auto focus
}
///
/// 加载网络配置文件
///
///
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());
}
///
/// 加载本地配置文件
///
///
IEnumerator LoadLocalFile()
{
bool isVuforiaEnabled = VuforiaRuntimeUtilities.IsVuforiaEnabled();
Debug.Log(isVuforiaEnabled);
// logtest.text = "isVuforiaEnabled " + isVuforiaEnabled + " m_isLoaded " + m_isLoaded;
if (isVuforiaEnabled && m_isLoaded == false)
{
if (m_dataSet == null)
{
m_tracker = TrackerManager.Instance.GetTracker();
m_dataSet = m_tracker.CreateDataSet();
}
Debug.Log("LoadData " + m_localFilePath);
// logtest.text = "LoadData " + m_localFilePath;
ObjectTracker objectTracker = TrackerManager.Instance.GetTracker();
objectTracker.Stop();
if (m_dataSet.Load(m_localFilePath, VuforiaUnity.StorageType.STORAGE_ABSOLUTE))
{
Debug.Log("LoadData OVer " + m_localFilePath);
// logtest.text = "LoadData OVer " + m_localFilePath;
m_isLoaded = true;
m_tracker.ActivateDataSet(m_dataSet);
// errortext.text = m_tracker.ActivateDataSet(m_dataSet).ToString();
objectTracker.Start();
UpdateImageTarget();
}
else
{
m_isLoaded = false;
}
}
WWW www = new WWW("file:///" + m_localFilePath);
yield return www;
}
///
/// 修改Imagetarget 的名称
///
void UpdateImageTarget()
{
List imagetargetNameList = new List();
m_imageTargetBehaviours = FindObjectsOfType();
// logtest.text = m_imageTargetBehaviours.Length.ToString();
for (int i = 0; i < m_imageTargetBehaviours.Length; i++)
{
ImageTargetBehaviour imageTargetBehaviour = m_imageTargetBehaviours[i];
//logtest.text = logtest.text+" "+ m_imageTargetBehaviours[i].name;
imageTargetBehaviour.name = "ImageTarget_" + imageTargetBehaviour.ImageTarget.Name;
//imageTargetBehaviour.gameObject.AddComponent();
//读取 DefaultTrackableEventHandler.lua
imageTargetBehaviour.gameObject.AddComponent();
//imageTargetBehaviour.gameObject.AddComponent();
imageTargetBehaviour.gameObject.AddComponent();
imageTargetBehaviour.gameObject.AddComponent();
imagetargetNameList.Add(imageTargetBehaviour.name);
//listShowObj[i].transform.parent = imageTargetBehaviour.transform;
//listShowObj[i].transform.localPosition = Vector3.zero;
VuforialFindImageAction imageaction = imageTargetBehaviour.gameObject.GetComponent();
GameObject obj = new GameObject("Cube");
obj.transform.parent = imageaction.transform;
obj.transform.localEulerAngles = new Vector3(90, 0, 0);
obj.transform.localPosition = Vector3.zero;
imageaction.num = i;
imageaction.targetObj = obj.transform;
imageaction.pointObj = 0;
// VuforialControl.Instance.list_ImageAction.Add(imageaction);
}
OnImageTargerLoadedEvent(imagetargetNameList.ToArray());
}
///
/// 获取所有的识别图对象
///
///
public ImageTargetBehaviour[] GetImageTargetBehaviours()
{
return m_imageTargetBehaviours;
}
protected virtual void OnImageTargerLoadedEvent(string[] obj)
{
var handler = ImageTargerLoadedEvent;
if (handler != null)
{
handler(obj);
}
}
}