ImageTargetManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using Vuforia;
  8. using QFramework;
  9. /// <summary>
  10. /// 识别图管理器
  11. /// </summary>
  12. public class ImageTargetManager : MonoSingleton<ImageTargetManager>
  13. {
  14. //public Text logtest;
  15. //public Text errortext;
  16. // public List<GameObject> listShowObj;
  17. /// <summary>
  18. /// 识别图加载完成
  19. /// </summary>
  20. public event Action<string[]> ImageTargerLoadedEvent;
  21. /// <summary>
  22. /// 本地文件路径
  23. /// </summary>
  24. private string m_localFilePath;
  25. /// <summary>
  26. /// 是否加载过
  27. /// </summary>
  28. private bool m_isLoaded;
  29. /// <summary>
  30. /// 数据集
  31. /// </summary>
  32. private DataSet m_dataSet = null;
  33. /// <summary>
  34. /// 识别器
  35. /// </summary>
  36. private ObjectTracker m_tracker;
  37. /// <summary>
  38. /// 识别图集合对象
  39. /// </summary>
  40. private ImageTargetBehaviour[] m_imageTargetBehaviours;
  41. private void Start()
  42. {
  43. // Debug.Log(" Inited " + m_isLoaded);
  44. m_isLoaded = false;
  45. //TODO 识别图相关信息加载
  46. //VuforiaBehaviour.Instance.RegisterVuforiaInitializedCallback(VuforiaInitedCallBack);
  47. try
  48. {
  49. VuforiaARController.Instance.RegisterVuforiaInitializedCallback(VuforiaInitedCallBack);
  50. VuforiaARController.Instance.RegisterVuforiaStartedCallback(VuforiaStaredtCallBack);
  51. }
  52. catch (Exception e)
  53. {
  54. // errortext.text = e.Message;
  55. }
  56. }
  57. /// <summary>
  58. /// 高通初始化完毕回调
  59. /// </summary>
  60. void VuforiaInitedCallBack()
  61. {
  62. m_localFilePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".xml";
  63. Debug.Log( " Inited@@@ "+m_localFilePath+" " +File.Exists(m_localFilePath));
  64. //logtest.text = " Inited@@@ " + m_localFilePath + " " + File.Exists(m_localFilePath);
  65. if (File.Exists(m_localFilePath))
  66. {
  67. //Load Local
  68. StartCoroutine(LoadLocalFile());
  69. }
  70. else
  71. {
  72. //Load NetWork
  73. //StartCoroutine(LoadNetworkFile());
  74. }
  75. }
  76. void VuforiaStaredtCallBack()
  77. {
  78. CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
  79. // errortext.text = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO).ToString(); //auto focus
  80. }
  81. /// <summary>
  82. /// 加载网络配置文件
  83. /// </summary>
  84. /// <returns></returns>
  85. private IEnumerator LoadNetworkFile()
  86. {
  87. WWW wwwdat = new WWW(AppConfigConst.IMAGE_TARGET_FILE_PATH + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".dat");
  88. yield return wwwdat;
  89. Debug.Log(wwwdat.url);
  90. WWW wwwxml = new WWW(AppConfigConst.IMAGE_TARGET_FILE_PATH + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".xml");
  91. yield return wwwxml;
  92. File.WriteAllBytes(Application.persistentDataPath + "/" + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".dat", wwwdat.bytes);
  93. File.WriteAllBytes(Application.persistentDataPath + "/" + AppConfigConst.IMAGE_TARGET_FILE_NAME + ".xml", wwwxml.bytes);
  94. StartCoroutine(LoadLocalFile());
  95. }
  96. /// <summary>
  97. /// 加载本地配置文件
  98. /// </summary>
  99. /// <returns></returns>
  100. IEnumerator LoadLocalFile()
  101. {
  102. bool isVuforiaEnabled = VuforiaRuntimeUtilities.IsVuforiaEnabled();
  103. Debug.Log(isVuforiaEnabled);
  104. // logtest.text = "isVuforiaEnabled " + isVuforiaEnabled + " m_isLoaded " + m_isLoaded;
  105. if (isVuforiaEnabled && m_isLoaded == false)
  106. {
  107. if (m_dataSet == null)
  108. {
  109. m_tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
  110. m_dataSet = m_tracker.CreateDataSet();
  111. }
  112. Debug.Log("LoadData " + m_localFilePath);
  113. // logtest.text = "LoadData " + m_localFilePath;
  114. ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
  115. objectTracker.Stop();
  116. if (m_dataSet.Load(m_localFilePath, VuforiaUnity.StorageType.STORAGE_ABSOLUTE))
  117. {
  118. Debug.Log("LoadData OVer " + m_localFilePath);
  119. // logtest.text = "LoadData OVer " + m_localFilePath;
  120. m_isLoaded = true;
  121. m_tracker.ActivateDataSet(m_dataSet);
  122. // errortext.text = m_tracker.ActivateDataSet(m_dataSet).ToString();
  123. objectTracker.Start();
  124. UpdateImageTarget();
  125. }
  126. else
  127. {
  128. m_isLoaded = false;
  129. }
  130. }
  131. WWW www = new WWW("file:///" + m_localFilePath);
  132. yield return www;
  133. }
  134. /// <summary>
  135. /// 修改Imagetarget 的名称
  136. /// </summary>
  137. void UpdateImageTarget()
  138. {
  139. List<string> imagetargetNameList = new List<string>();
  140. m_imageTargetBehaviours = FindObjectsOfType<ImageTargetBehaviour>();
  141. // logtest.text = m_imageTargetBehaviours.Length.ToString();
  142. for (int i = 0; i < m_imageTargetBehaviours.Length; i++)
  143. {
  144. ImageTargetBehaviour imageTargetBehaviour = m_imageTargetBehaviours[i];
  145. //logtest.text = logtest.text+" "+ m_imageTargetBehaviours[i].name;
  146. imageTargetBehaviour.name = "ImageTarget_" + imageTargetBehaviour.ImageTarget.Name;
  147. //imageTargetBehaviour.gameObject.AddComponent<DefaultTrackableEventHandler>();
  148. //读取 DefaultTrackableEventHandler.lua
  149. imageTargetBehaviour.gameObject.AddComponent<DefaultTrackableEventHandler>();
  150. //imageTargetBehaviour.gameObject.AddComponent<LuaTrackableEventHandler>();
  151. imageTargetBehaviour.gameObject.AddComponent<TurnOffBehaviour>();
  152. imageTargetBehaviour.gameObject.AddComponent<VuforialFindImageAction>();
  153. imagetargetNameList.Add(imageTargetBehaviour.name);
  154. //listShowObj[i].transform.parent = imageTargetBehaviour.transform;
  155. //listShowObj[i].transform.localPosition = Vector3.zero;
  156. VuforialFindImageAction imageaction = imageTargetBehaviour.gameObject.GetComponent<VuforialFindImageAction>();
  157. GameObject obj = new GameObject("Cube");
  158. obj.transform.parent = imageaction.transform;
  159. obj.transform.localEulerAngles = new Vector3(90, 0, 0);
  160. obj.transform.localPosition = Vector3.zero;
  161. imageaction.num = i;
  162. imageaction.targetObj = obj.transform;
  163. imageaction.pointObj = 0;
  164. // VuforialControl.Instance.list_ImageAction.Add(imageaction);
  165. }
  166. OnImageTargerLoadedEvent(imagetargetNameList.ToArray());
  167. }
  168. /// <summary>
  169. /// 获取所有的识别图对象
  170. /// </summary>
  171. /// <returns></returns>
  172. public ImageTargetBehaviour[] GetImageTargetBehaviours()
  173. {
  174. return m_imageTargetBehaviours;
  175. }
  176. protected virtual void OnImageTargerLoadedEvent(string[] obj)
  177. {
  178. var handler = ImageTargerLoadedEvent;
  179. if (handler != null)
  180. {
  181. handler(obj);
  182. }
  183. }
  184. }