LoadManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = 10;
  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.uid ,model.modelType);
  46. downList.Add(dName, dli);
  47. }
  48. downList[dName].onProgress += (float f) => {
  49. onProgress.Invoke(f);
  50. };
  51. downList[dName].callback += (bool b) => {
  52. if(b)
  53. {
  54. if (!downList[dName].isAction)
  55. {
  56. //涓嬭浇鎴愬姛骞舵棤澶勭悊
  57. switch (model.modelType)
  58. {
  59. case ModelType.Image:
  60. break;
  61. case ModelType.Video:
  62. break;
  63. case ModelType.ABModel:
  64. break;
  65. }
  66. Debug.Log(" LoadManager "+ dName + " Length : "+ downList[dName].downLoadData.Length);
  67. model.SetData(downList[dName].downLoadData);
  68. }
  69. }
  70. else
  71. {
  72. //涓嬭浇澶辫触
  73. }
  74. };
  75. }
  76. public void loadVuforia(string xmlFile, string datFile, Action<bool> xmlcallback, Action<bool> datcallback)
  77. {
  78. GameObject xmlFileDownObj = new GameObject("VufroiaTriggerDownLoad_xmlFile");
  79. DownLoadItem xmlFileItem = xmlFileDownObj.AddComponent<DownLoadItem>();
  80. xmlFileItem.Init(xmlFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.Text);
  81. xmlFileItem.onProgress += (float f) => {
  82. };
  83. xmlFileItem.callback += (bool b) =>
  84. {
  85. if (b)
  86. {
  87. File.WriteAllBytes(VufroiaTrigger.LoaclxmlFile, xmlFileItem.downLoadData);
  88. }
  89. xmlcallback.Invoke(b);
  90. };
  91. GameObject datFileObj = new GameObject("VufroiaTriggerDownLoad_datFile");
  92. DownLoadItem datFileItem = datFileObj.AddComponent<DownLoadItem>();
  93. datFileItem.Init(datFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.Text);
  94. datFileItem.onProgress += (float f) => {
  95. };
  96. datFileItem.callback += (bool b) =>
  97. {
  98. if (b)
  99. {
  100. File.WriteAllBytes(VufroiaTrigger.LoacldatFile, datFileItem.downLoadData);
  101. }
  102. datcallback.Invoke(b);
  103. };
  104. }
  105. public string getLoadName(string url, string version)
  106. {
  107. return url + "_" + version;
  108. }
  109. public Queue<DownLoadItem> downloadQueueList = new Queue<DownLoadItem>();
  110. }