LoadManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Debug.Log("正在下载=ct==> ==>" + nowLoad);
  15. if (downloadQueueList.Count>0&& nowLoad< maxLoad)
  16. {
  17. int ct = downloadQueueList.Count;
  18. Debug.Log("正在下载=ct==>" + ct+" ==>" + nowLoad);
  19. for (int i = 0; i < ct; i++)
  20. {
  21. DownLoadItem item = downloadQueueList.Dequeue();
  22. Debug.Log("正在下载===>"+ item.name);
  23. item.onProgress += (float f) => {
  24. Debug.Log("下载进度===>" + f);
  25. };
  26. item.callback += (bool b) => {
  27. Debug.Log("下载完成===>" + nowLoad);
  28. nowLoad--;
  29. };
  30. GameScene.Instance.StartCoroutine( item.DownloadFile());
  31. nowLoad++;
  32. if(nowLoad>= maxLoad)
  33. {
  34. break;
  35. }
  36. }
  37. }
  38. }, 1,-1);
  39. }
  40. public Dictionary<string, DownLoadItem> downList = new Dictionary<string, DownLoadItem>();
  41. public void load(ModelItem model, Action<float> onProgress, Action<GameObject> callback)
  42. {
  43. string dName = getLoadName(model.url, model.Version);
  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. }
  72. }
  73. else
  74. {
  75. //涓嬭浇澶辫触
  76. }
  77. };
  78. }
  79. public void loadVuforia(string xmlFile, string datFile, Action<bool> xmlcallback, Action<bool> datcallback)
  80. {
  81. GameObject xmlFileDownObj = new GameObject("VufroiaTriggerDownLoad_xmlFile");
  82. DownLoadItem xmlFileItem = xmlFileDownObj.AddComponent<DownLoadItem>();
  83. xmlFileItem.Init(xmlFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.Text);
  84. xmlFileItem.onProgress += (float f) => {
  85. };
  86. xmlFileItem.callback += (bool b) =>
  87. {
  88. if (b)
  89. {
  90. File.WriteAllBytes(VufroiaTrigger.LoaclxmlFile, xmlFileItem.downLoadData);
  91. }
  92. xmlcallback.Invoke(b);
  93. };
  94. GameObject datFileObj = new GameObject("VufroiaTriggerDownLoad_datFile");
  95. DownLoadItem datFileItem = datFileObj.AddComponent<DownLoadItem>();
  96. datFileItem.Init(datFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.Text);
  97. datFileItem.onProgress += (float f) => {
  98. };
  99. datFileItem.callback += (bool b) =>
  100. {
  101. if (b)
  102. {
  103. File.WriteAllBytes(VufroiaTrigger.LoacldatFile, datFileItem.downLoadData);
  104. }
  105. datcallback.Invoke(b);
  106. };
  107. }
  108. public string getLoadName(string url, string version)
  109. {
  110. return url + "_" + version;
  111. }
  112. public Queue<DownLoadItem> downloadQueueList = new Queue<DownLoadItem>();
  113. }