LoadManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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+ " model.url==>"+ model.url);
  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,model.updateTime);
  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. if(downList[dName].downLoadData!=null)
  70. {
  71. Debug.Log(" LoadManager " + dName + " Length : " + downList[dName].downLoadData.Length);
  72. model.SetData(downList[dName].downLoadData);
  73. }else if(model.prefabModel==null)
  74. {
  75. model.prefabModel = new GameObject("空物体"+ downList[dName].id);
  76. }
  77. callback.Invoke(model.prefabModel);
  78. }
  79. }
  80. else
  81. {
  82. //涓嬭浇澶辫触
  83. }
  84. };
  85. }
  86. public void loadVuforia(string xmlFile, string datFile, Action<bool> xmlcallback, Action<bool> datcallback)
  87. {
  88. if (!Directory.Exists(Application.persistentDataPath + "/StreamingAssets"))
  89. {
  90. Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets");
  91. if(!Directory.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia"))
  92. {
  93. Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets/Vuforia");
  94. }
  95. }
  96. GameObject xmlFileDownObj = new GameObject("VufroiaTriggerDownLoad_xmlFile");
  97. DownLoadItem xmlFileItem = xmlFileDownObj.AddComponent<DownLoadItem>();
  98. xmlFileItem.Init(xmlFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial, long.Parse(GameManager.Instance.m_scene.updateTime));
  99. xmlFileItem.onProgress += (float f) => {
  100. };
  101. xmlFileItem.callback += (bool b) =>
  102. {
  103. Debug.Log("Vuforia 下载完成");
  104. if (b)
  105. {
  106. Debug.Log("Vuforia 下载完成 ===>" + VufroiaTrigger.LoacldatFile);
  107. // File.WriteAllBytes(VufroiaTrigger.LoaclxmlFile, xmlFileItem.downLoadData);
  108. }
  109. xmlcallback.Invoke(b);
  110. };
  111. GameObject datFileObj = new GameObject("VufroiaTriggerDownLoad_datFile");
  112. DownLoadItem datFileItem = datFileObj.AddComponent<DownLoadItem>();
  113. datFileItem.Init(datFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial, long.Parse(GameManager.Instance.m_scene.updateTime));
  114. datFileItem.onProgress += (float f) => {
  115. };
  116. datFileItem.callback += (bool b) =>
  117. {
  118. Debug.Log("Vuforia 下载完成");
  119. if (b)
  120. {
  121. Debug.Log("Vuforia 下载完成 ===>"+ VufroiaTrigger.LoacldatFile);
  122. // File.WriteAllBytes(VufroiaTrigger.LoacldatFile, datFileItem.downLoadData);
  123. }
  124. datcallback.Invoke(b);
  125. };
  126. }
  127. public string getLoadName(string url, string version)
  128. {
  129. return url + "_" + version;
  130. }
  131. public Queue<DownLoadItem> downloadQueueList = new Queue<DownLoadItem>();
  132. }