LoadManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using XRTool.Util;
  7. public class LoadManager : Singleton<LoadManager>
  8. {
  9. const int maxLoad = 3;
  10. int nowLoad = 0;
  11. public LoadManager()
  12. {
  13. TimerMgr.Instance.CreateTimer(() => {
  14. if (downloadQueueList.Count>0&& nowLoad< maxLoad)
  15. {
  16. int ct = downloadQueueList.Count;
  17. // Debug.LogError(" downloadQueueList =====> " + ct);
  18. for (int i = 0; i < ct; i++)
  19. {
  20. DownLoadItem item = downloadQueueList.Dequeue();
  21. item.onProgress += (float f) => {
  22. Debug.Log("下载进度===>" + f);
  23. };
  24. item.callback += (bool b) => {
  25. Debug.Log("下载完成===>" + nowLoad);
  26. nowLoad--;
  27. };
  28. // GameScene.Instance.StartCoroutine( item.DownloadFile());
  29. item.DownloadFileMsg();
  30. nowLoad++;
  31. if(nowLoad>= maxLoad)
  32. {
  33. break;
  34. }
  35. }
  36. }
  37. }, 1,-1);
  38. }
  39. public Dictionary<string, List<Action<float>>> onProgresslist = new Dictionary<string, List<Action<float>>>();
  40. public Dictionary<string, List<Action<byte[]>>> callbacklist = new Dictionary<string, List<Action<byte[]>>>();
  41. public void setaction(string id,Action<float> onProgress, Action<byte[]> callback)
  42. {
  43. if(onProgresslist.ContainsKey(id))
  44. {
  45. onProgresslist[id].Add(onProgress);
  46. }
  47. else
  48. {
  49. List<Action<float>> f = new List<Action<float>>();
  50. f.Add(onProgress);
  51. onProgresslist.Add(id, f);
  52. }
  53. if (callbacklist.ContainsKey(id))
  54. {
  55. callbacklist[id].Add(callback);
  56. }
  57. else
  58. {
  59. List < Action <byte[]>> c = new List<Action<byte[]>>();
  60. c.Add(callback);
  61. callbacklist.Add(id, c);
  62. }
  63. }
  64. public Dictionary<string, DownLoadItem> downList = new Dictionary<string, DownLoadItem>();
  65. public void load(ModelItem model, Action<float> onProgress, Action<byte[]> callback)
  66. {
  67. string dName = getLoadName(model.url, model.Version);
  68. Debug.Log("dName ======> " + dName+ " model.url==>"+ model.url);
  69. if (!downList.ContainsKey(dName))
  70. {
  71. GameObject obj = new GameObject(dName);
  72. DownLoadItem dli = obj.AddComponent<DownLoadItem>();
  73. dli.Init(model.url, model.uid ,model.modelType,model.updateTime);
  74. downList.Add(dName, dli);
  75. }
  76. setaction(dName, onProgress, callback);
  77. downList[dName].onProgress += (float f) => {
  78. for (int i = 0; i < onProgresslist[dName].Count; i++)
  79. {
  80. onProgresslist[dName][i].Invoke(f);
  81. }
  82. };
  83. downList[dName].callback += (bool b) => {
  84. if (b)
  85. {
  86. if (!downList[dName].isAction)
  87. {
  88. //涓嬭浇鎴愬姛骞舵棤澶勭悊
  89. switch (model.modelType)
  90. {
  91. case ModelType.Image:
  92. break;
  93. case ModelType.Video:
  94. break;
  95. case ModelType.ABModel:
  96. break;
  97. }
  98. if(downList[dName].downLoadData!=null)
  99. {
  100. Debug.Log(" LoadManager " + dName + " Length : " + downList[dName].downLoadData.Length);
  101. model.SetData(downList[dName].downLoadData, downList[dName].data);
  102. }else if(model.prefabModel==null)
  103. {
  104. model.prefabModel = new GameObject("空物体"+ downList[dName].id);
  105. // model.initFrist();
  106. }
  107. for (int i = 0; i < callbacklist[dName].Count; i++)
  108. {
  109. callbacklist[dName][i].Invoke(downList[dName].downLoadData);
  110. }
  111. }
  112. }
  113. else
  114. {
  115. //涓嬭浇澶辫触
  116. }
  117. };
  118. }
  119. public GameObject datFileObj;
  120. public GameObject xmlFileDownObj;
  121. public void loadVuforia(string xmlFile, string datFile, Action<bool> xmlcallback, Action<bool> datcallback)
  122. {
  123. if (File.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.xml"))
  124. {
  125. File.Delete(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.xml");
  126. }
  127. if (File.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.dat"))
  128. {
  129. File.Delete(Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia.dat");
  130. }
  131. if (!Directory.Exists(Application.persistentDataPath + "/StreamingAssets"))
  132. {
  133. Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets");
  134. if(!Directory.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia"))
  135. {
  136. Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets/Vuforia");
  137. }
  138. }
  139. xmlFileDownObj = new GameObject("VufroiaTriggerDownLoad_xmlFile");
  140. DownLoadItem xmlFileItem = xmlFileDownObj.AddComponent<DownLoadItem>();
  141. xmlFileItem.Init(xmlFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial, long.Parse(GameManager.Instance.m_scene.updateTime));
  142. xmlFileItem.onProgress += (float f) => {
  143. };
  144. xmlFileItem.callback += (bool b) =>
  145. {
  146. Debug.Log("Vuforia 下载完成");
  147. if (b)
  148. {
  149. Debug.Log("Vuforia 下载完成 ===>" + VufroiaTrigger.LoacldatFile);
  150. // File.WriteAllBytes(VufroiaTrigger.LoaclxmlFile, xmlFileItem.downLoadData);
  151. }
  152. xmlcallback.Invoke(b);
  153. };
  154. datFileObj = new GameObject("VufroiaTriggerDownLoad_datFile");
  155. DownLoadItem datFileItem = datFileObj.AddComponent<DownLoadItem>();
  156. datFileItem.Init(datFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial, long.Parse(GameManager.Instance.m_scene.updateTime));
  157. datFileItem.onProgress += (float f) => {
  158. };
  159. datFileItem.callback += (bool b) =>
  160. {
  161. Debug.Log("Vuforia 下载完成");
  162. if (b)
  163. {
  164. Debug.Log("Vuforia 下载完成 ===>"+ VufroiaTrigger.LoacldatFile);
  165. // File.WriteAllBytes(VufroiaTrigger.LoacldatFile, datFileItem.downLoadData);
  166. }
  167. datcallback.Invoke(b);
  168. };
  169. }
  170. public string getLoadName(string url, string version)
  171. {
  172. return url + "_" + version;
  173. }
  174. public DownLoadItem getdownloaditem(ModelItem mi)
  175. {
  176. Debug.Log("getdownloaditem=====>"+getLoadName(mi.url, mi.Version));
  177. if(downList.ContainsKey(getLoadName(mi.url, mi.Version)))
  178. {
  179. DownLoadItem dii = downList[getLoadName(mi.url, mi.Version)];
  180. return dii;
  181. }
  182. else
  183. {
  184. return null;
  185. }
  186. }
  187. public Queue<DownLoadItem> downloadQueueList = new Queue<DownLoadItem>();
  188. }