LoadManager.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using QFramework;
  5. using System.IO;
  6. using Newtonsoft.Json;
  7. using UnityEngine.Networking;
  8. public class LoadManager : MonoSingleton<LoadManager>
  9. {
  10. /// <summary>
  11. /// 本次运行已经加载过的物体
  12. /// </summary>
  13. private List<MaterialData> listMaterialData;
  14. /// <summary>
  15. /// 下载过的物体
  16. /// </summary>
  17. // private List<MaterialObjValue> listLoadData;
  18. private List<DownLoadMaterial> listDownLoad;
  19. private bool isLoading;
  20. public bool isSave = false;
  21. private void Start()
  22. {
  23. // PlayerPrefs.DeleteAll();
  24. // PlayerPrefs.DeleteAll();
  25. isLoading = true;
  26. listMaterialData = new List<MaterialData>();
  27. listDownLoad = new List<DownLoadMaterial>();
  28. if (PlayerPrefs.HasKey("LoadData111"))
  29. {
  30. string str = PlayerPrefs.GetString("LoadData111");
  31. listDownLoad = JsonConvert.DeserializeObject<List<DownLoadMaterial>>(str);
  32. if (listDownLoad == null)
  33. listDownLoad = new List<DownLoadMaterial>();
  34. Debug.Log(listDownLoad.Count);
  35. for (int i = 0; i < listDownLoad.Count; i++)
  36. {
  37. Debug.Log(listDownLoad[i].mObj.name + " " + listDownLoad[i].updateTime);
  38. }
  39. }
  40. else
  41. {
  42. listDownLoad = new List<DownLoadMaterial>();
  43. }
  44. }
  45. private void Update()
  46. {
  47. if(isSave)
  48. {
  49. isSave = false;
  50. SaveData();
  51. }
  52. }
  53. /// <summary>
  54. /// 初始 下载所有需要的素材
  55. /// </summary>
  56. /// <param name="listObjValue"></param>
  57. public void InitLoad(List<DownLoadMaterial> listObjValue)
  58. {
  59. bool state;
  60. //int num;
  61. Debug.Log("Init AllDownLoad " + listObjValue.Count);
  62. try
  63. {
  64. // 初始化
  65. for (int i = 0; i < listObjValue.Count; i++)
  66. {
  67. state = true;
  68. if (listDownLoad != null)
  69. for (int j = 0; j < listDownLoad.Count; j++)
  70. {
  71. // Debug.Log(listObjValue[i].mObj.DownloadPath + " NAME " + listDownLoad[j].mObj.DownloadPath);
  72. if (listDownLoad[j].mObj.DownloadPath == listObjValue[i].mObj.DownloadPath && listDownLoad[j].updateTime == listObjValue[i].updateTime)
  73. {
  74. Debug.Log(listObjValue[i].mObj.name+" False ");
  75. state = false;
  76. break;
  77. }
  78. }
  79. if (state)
  80. {
  81. if (listObjValue[i].mObj.DownloadPath.IsNotNullAndEmpty() && listObjValue[i].mObj.type != "4")
  82. {
  83. DownLoadManage.Instance.AddDownLoadData(listObjValue[i]);
  84. isLoading = true;
  85. }
  86. }
  87. }
  88. LoadShow.Instance.allDownLoad = listObjValue.Count;
  89. LoadShow.Instance.ViewShow(true);
  90. Debug.Log("AllDownLoad " + DownLoadManage.Instance.AllDownLoad);
  91. if (DownLoadManage.Instance.AllDownLoad == 0)
  92. {
  93. Debug.Log("AllDownLoad " + DownLoadManage.Instance.AllDownLoad);
  94. GameManager.Instance.state = true;
  95. }
  96. }
  97. catch (System.Exception)
  98. {
  99. ErrorLogPanel.Instance.Show(" 初始化下载信息出现错误 ");
  100. }
  101. }
  102. /// <summary>
  103. /// 加载素材
  104. /// </summary>
  105. /// <param name="value"></param>
  106. /// <param name="updateTime"></param>
  107. public void LoadMaterial(MaterialObjValue value , int updateTime)
  108. {
  109. try
  110. {
  111. for (int i = 0; i < listMaterialData.Count; i++)
  112. {
  113. // 已经加载过
  114. if (listMaterialData[i].mObj.DownloadPath == value.DownloadPath)
  115. {
  116. SendMsg(value.DownloadPath, listMaterialData[i].value);
  117. return;
  118. }
  119. }
  120. Debug.Log(listDownLoad.Count);
  121. for (int i = 0; i < listDownLoad.Count; i++)
  122. {
  123. // 已经下载了,无需更新,直接加载本地资源
  124. if (listDownLoad[i].mObj.DownloadPath == value.DownloadPath && listDownLoad[i].updateTime == updateTime)
  125. {
  126. object obj;
  127. switch ((MaterialType)int.Parse(value.type))
  128. {
  129. case MaterialType.Video:
  130. string path = listDownLoad[i].mObj.localSavePath;
  131. obj = path;
  132. SendMsg(value.DownloadPath, obj);
  133. listMaterialData.Add(new MaterialData(value, value.localSavePath));
  134. break;
  135. case MaterialType.Model:
  136. DownLoadManage.Instance.AddLocalData(new DownLoadMaterial(value, updateTime));
  137. break;
  138. case MaterialType.Image:
  139. DownLoadManage.Instance.AddLocalData(new DownLoadMaterial(value, updateTime));
  140. break;
  141. default:
  142. break;
  143. }
  144. return;
  145. }
  146. }
  147. // 需要下载
  148. DownLoadManage.Instance.AddDownLoadData(new DownLoadMaterial(value, updateTime));
  149. }
  150. catch (System.Exception e)
  151. {
  152. ErrorLogPanel.Instance.Show(" 加载素材出现错误 " + e.Message);
  153. throw;
  154. }
  155. }
  156. /// <summary>
  157. /// 发送下载消息
  158. /// </summary>
  159. /// <param name="name"></param>
  160. /// <param name="value"></param>
  161. public void SendMsg(string name, object value)
  162. {
  163. MsgHandler.SendMsg(name, new Msg("", value));
  164. }
  165. /// <summary>
  166. /// 下载结束
  167. /// </summary>
  168. /// <param name="mObj"></param>
  169. /// <param name="value"></param>
  170. public void DownLoadEnd( DownLoadMaterial mObj,object value )
  171. {
  172. // Debug.Log(listMaterialData.Count +" ++++++++++++++");
  173. listDownLoad.Add(mObj);
  174. // SaveData();
  175. isSave = true;
  176. listMaterialData.Add(new MaterialData(mObj.mObj, value));
  177. SendMsg(mObj.mObj.DownloadPath, value);
  178. // Debug.Log(mObj.mObj.name);
  179. // 后续根据实际测试结果来决定是否需要保存 模型的object文件 (可能会造成内存占用过大的问题)
  180. }
  181. /// <summary>
  182. /// 获取已下载素材
  183. /// </summary>
  184. /// <param name="downLoadPath"></param>
  185. /// <param name="updatetime"></param>
  186. /// <returns></returns>
  187. public object GetMaterial(string downLoadPath , long updatetime)
  188. {
  189. // Debug.Log(listMaterialData.Count);
  190. for (int i = 0; i < listMaterialData.Count; i++)
  191. {
  192. // Debug.Log(listMaterialData[i].mObj.DownloadPath +" "+downLoadPath);
  193. if(listMaterialData[i].mObj.DownloadPath == downLoadPath)
  194. {
  195. return listMaterialData[i].value;
  196. }
  197. }
  198. return null;
  199. }
  200. /// <summary>
  201. /// 初始加载结束
  202. /// </summary>
  203. public void DownLoadFinish()
  204. {
  205. if (isLoading)
  206. isLoading = false;
  207. // Debug.Log("DownLoadFinish @@@@@@@@@@@@@@@@@@@@@@@");
  208. GameManager.Instance.state = true;
  209. }
  210. protected void OnDestroy()
  211. {
  212. SaveData();
  213. }
  214. /// <summary>
  215. /// 保存数据至本地 离线版
  216. /// </summary>
  217. private void SaveData()
  218. {
  219. string str = JsonConvert.SerializeObject(listDownLoad);
  220. PlayerPrefs.SetString("LoadData111", str);
  221. }
  222. }
  223. public class DownLoadMaterial
  224. {
  225. public MaterialObjValue mObj { get; set; }
  226. public int updateTime { get; set; }
  227. public DownLoadMaterial(MaterialObjValue mObj, int updateTime)
  228. {
  229. this.mObj = mObj;
  230. this.updateTime = updateTime;
  231. }
  232. }
  233. public class MaterialData
  234. {
  235. public MaterialObjValue mObj;
  236. public object value;
  237. public MaterialData(MaterialObjValue mObj, object value)
  238. {
  239. this.mObj = mObj;
  240. this.value = value;
  241. }
  242. }