LoadManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, DownLoadItem> downList = new Dictionary<string, DownLoadItem>();
  40. public void load(ModelItem model, Action<float> onProgress, Action<GameObject> callback)
  41. {
  42. string dName = getLoadName(model.url, model.Version);
  43. Debug.Log("dName ======> " + dName);
  44. if (!downList.ContainsKey(dName))
  45. {
  46. GameObject obj = new GameObject(dName);
  47. DownLoadItem dli = obj.AddComponent<DownLoadItem>();
  48. dli.Init(model.url, model.uid ,model.modelType);
  49. downList.Add(dName, dli);
  50. }
  51. downList[dName].onProgress += (float f) => {
  52. onProgress.Invoke(f);
  53. };
  54. downList[dName].callback += (bool b) => {
  55. if(b)
  56. {
  57. if (!downList[dName].isAction)
  58. {
  59. //涓嬭浇鎴愬姛骞舵棤澶勭悊
  60. switch (model.modelType)
  61. {
  62. case ModelType.Image:
  63. break;
  64. case ModelType.Video:
  65. break;
  66. case ModelType.ABModel:
  67. break;
  68. }
  69. Debug.Log(" LoadManager "+ dName + " Length : "+ downList[dName].downLoadData.Length);
  70. model.SetData(downList[dName].downLoadData);
  71. callback.Invoke(model.prefabModel);
  72. }
  73. }
  74. else
  75. {
  76. //涓嬭浇澶辫触
  77. }
  78. };
  79. }
  80. public void loadVuforia(string xmlFile, string datFile, Action<bool> xmlcallback, Action<bool> datcallback)
  81. {
  82. if (!Directory.Exists(Application.persistentDataPath + "/StreamingAssets"))
  83. {
  84. Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets");
  85. if(!Directory.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia"))
  86. {
  87. Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets/Vuforia");
  88. }
  89. }
  90. GameObject xmlFileDownObj = new GameObject("VufroiaTriggerDownLoad_xmlFile");
  91. DownLoadItem xmlFileItem = xmlFileDownObj.AddComponent<DownLoadItem>();
  92. xmlFileItem.Init(xmlFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial);
  93. xmlFileItem.onProgress += (float f) => {
  94. };
  95. xmlFileItem.callback += (bool b) =>
  96. {
  97. Debug.Log("Vuforia 下载完成");
  98. if (b)
  99. {
  100. Debug.Log("Vuforia 下载完成 ===>" + VufroiaTrigger.LoacldatFile);
  101. // File.WriteAllBytes(VufroiaTrigger.LoaclxmlFile, xmlFileItem.downLoadData);
  102. }
  103. xmlcallback.Invoke(b);
  104. };
  105. GameObject datFileObj = new GameObject("VufroiaTriggerDownLoad_datFile");
  106. DownLoadItem datFileItem = datFileObj.AddComponent<DownLoadItem>();
  107. datFileItem.Init(datFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial);
  108. datFileItem.onProgress += (float f) => {
  109. };
  110. datFileItem.callback += (bool b) =>
  111. {
  112. Debug.Log("Vuforia 下载完成");
  113. if (b)
  114. {
  115. Debug.Log("Vuforia 下载完成 ===>"+ VufroiaTrigger.LoacldatFile);
  116. // File.WriteAllBytes(VufroiaTrigger.LoacldatFile, datFileItem.downLoadData);
  117. }
  118. datcallback.Invoke(b);
  119. };
  120. }
  121. public string getLoadName(string url, string version)
  122. {
  123. return url + "_" + version;
  124. }
  125. public Queue<DownLoadItem> downloadQueueList = new Queue<DownLoadItem>();
  126. }