LoadManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = 5;
  10. int nowLoad = 0;
  11. public LoadManager()
  12. {
  13. TimerMgr.Instance.CreateTimer(() => {
  14. if(downloadQueueList.Count>0&& nowLoad< maxLoad)
  15. {
  16. for (int i = 0; i < downloadQueueList.Count; i++)
  17. {
  18. DownLoadItem item = downloadQueueList.Dequeue();
  19. Debug.Log("正在下载===>"+ item.name);
  20. item.onProgress += (float f) => {
  21. Debug.Log("下载进度===>" + f);
  22. };
  23. item.callback += (bool b) => {
  24. Debug.Log("下载完成===>" );
  25. nowLoad--;
  26. };
  27. GameScene.Instance.StartCoroutine( item.DownloadFile());
  28. nowLoad++;
  29. if(nowLoad>= maxLoad)
  30. {
  31. break;
  32. }
  33. }
  34. }
  35. }, -1);
  36. }
  37. public Dictionary<string, DownLoadItem> downList = new Dictionary<string, DownLoadItem>();
  38. public void load(ModelItem model, Action<float> onProgress, Action<GameObject> callback)
  39. {
  40. string dName = getLoadName(model.url, model.Version);
  41. if (!downList.ContainsKey(dName))
  42. {
  43. GameObject obj = new GameObject(dName);
  44. DownLoadItem dli = obj.AddComponent<DownLoadItem>();
  45. dli.Init(model.url, model.id);
  46. }
  47. downList[dName].onProgress += (float f) => {
  48. onProgress.Invoke(f);
  49. };
  50. downList[dName].callback += (bool b) => {
  51. if(b)
  52. {
  53. if (!downList[dName].isAction)
  54. {
  55. //下载成功并无处理
  56. switch (model.type)
  57. {
  58. case ModelItem.ModelType.Image:
  59. break;
  60. case ModelItem.ModelType.Video:
  61. break;
  62. case ModelItem.ModelType.ABModel:
  63. break;
  64. }
  65. }
  66. }
  67. else
  68. {
  69. //下载失败
  70. }
  71. };
  72. }
  73. public void loadVuforia(string xmlFile, string datFile, Action<bool> xmlcallback, Action<bool> datcallback)
  74. {
  75. GameObject xmlFileDownObj = new GameObject("VufroiaTriggerDownLoad_xmlFile");
  76. DownLoadItem xmlFileItem = xmlFileDownObj.AddComponent<DownLoadItem>();
  77. xmlFileItem.Init(xmlFile, "VufroiaTriggerDownLoad_xmlFile");
  78. xmlFileItem.onProgress += (float f) => {
  79. };
  80. xmlFileItem.callback += (bool b) =>
  81. {
  82. if (b)
  83. {
  84. File.WriteAllBytes(VufroiaTrigger.LoaclxmlFile, xmlFileItem.downLoadData);
  85. }
  86. xmlcallback.Invoke(b);
  87. };
  88. GameObject datFileObj = new GameObject("VufroiaTriggerDownLoad_datFile");
  89. DownLoadItem datFileItem = datFileObj.AddComponent<DownLoadItem>();
  90. datFileItem.Init(datFile, "VufroiaTriggerDownLoad_xmlFile");
  91. datFileItem.onProgress += (float f) => {
  92. };
  93. datFileItem.callback += (bool b) =>
  94. {
  95. if (b)
  96. {
  97. File.WriteAllBytes(VufroiaTrigger.LoacldatFile, datFileItem.downLoadData);
  98. }
  99. datcallback.Invoke(b);
  100. };
  101. }
  102. public string getLoadName(string url, string version)
  103. {
  104. return url + "_" + version;
  105. }
  106. public Queue<DownLoadItem> downloadQueueList = new Queue<DownLoadItem>();
  107. }