CLoaderData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Engine.CLoader
  5. {
  6. /// <summary>加载资源时的参数对象</summary>
  7. public class CLoaderData
  8. {
  9. /// <summary>资源名称数据</summary>
  10. public List<string> listAssetsName = null;
  11. /// <summary>下载进度</summary>
  12. public Dictionary<string, int> DictProgress = null;
  13. /// <summary>加载完成回调函数</summary>
  14. public OnDelegateLoadComplete onComplete = null;
  15. /// <summary>下载进度回调</summary>
  16. public OnDelegateLoadProgress onProgress = null;
  17. /// <summary>其他参数</summary>
  18. public object objectParam = null;
  19. /// <summary>下载的资源类型</summary>
  20. public AssetsType assetsType = AssetsType.Assets;
  21. /// <summary>数据源对象</summary>
  22. private List<CAssetsInfo> listAssetsSource = new List<CAssetsInfo>();
  23. /// <summary>是否已经被释放</summary>
  24. private bool isDispose = false;
  25. /// <summary>构造函数</summary>
  26. /// <param name="assetsName">List<string> 需要下载的资源包名列表</param>
  27. /// <param name="aType">资源类型</param>
  28. /// <param name="completeCallBack">下载完成回调</param>
  29. /// <param name="progressCallBack">下载进度回调</param>
  30. /// <param name="param">其他参数</param>
  31. public CLoaderData(List<string> assetsName = null, int aType = 1, OnDelegateLoadComplete completeCallBack = null, OnDelegateLoadProgress progressCallBack = null, object param = null)
  32. {
  33. listAssetsName = (assetsName != null) ? assetsName : new List<string>();
  34. onComplete = completeCallBack;
  35. onProgress = progressCallBack;
  36. objectParam = param;
  37. assetsType = (AssetsType)aType;
  38. }
  39. /// <summary>是否需要下载</summary>
  40. public bool IsNeedLoad
  41. {
  42. get{ return (listAssetsName != null && listAssetsName.Count > 0) ? true : false; }
  43. }
  44. /// <summary>资源的配置数据</summary>
  45. private List<CAssetsConfigInfo> assetsConfigList = new List<CAssetsConfigInfo>();
  46. /// <summary>资源的配置数据</summary>
  47. public List<CAssetsConfigInfo> AssetsConfigList
  48. {
  49. get
  50. {
  51. //资源类型是资源配置的资源可能没有依赖资源
  52. if (assetsConfigList.Count == 0 && assetsType != AssetsType.Config)
  53. {
  54. for (int i = 0; i < listAssetsName.Count; i++)
  55. {
  56. assetsConfigList.Add(CLoaderConfig.GetInstance().GetAssetsConfig(listAssetsName[i]));
  57. }
  58. }
  59. return assetsConfigList;
  60. }
  61. }
  62. /// <summary>加载一个GameObjec</summary>
  63. public UnityEngine.Object LoadObject(string assetsName, string objectName)
  64. {
  65. if (string.IsNullOrEmpty (assetsName) || string.IsNullOrEmpty (objectName))
  66. {
  67. return null;
  68. }
  69. CAssetsInfo assetsInfo = null;
  70. for(int i=0;i<listAssetsSource.Count;i++)
  71. {
  72. if (listAssetsSource[i].assetsName == assetsName)
  73. {
  74. assetsInfo = listAssetsSource[i];
  75. }
  76. }
  77. if (assetsInfo != null && assetsInfo.assetsBundle != null)
  78. {
  79. return assetsInfo.assetsBundle.LoadAsset(objectName);
  80. }
  81. else
  82. {
  83. return null;
  84. }
  85. }
  86. public void DebugAllAssets(string assetsName)
  87. {
  88. CAssetsInfo assetsInfo = null;
  89. for (int i = 0; i < listAssetsSource.Count; i++)
  90. {
  91. if (listAssetsSource[i].assetsName == assetsName)
  92. {
  93. assetsInfo = listAssetsSource[i];
  94. }
  95. }
  96. if(assetsInfo!=null)
  97. {
  98. UnityEngine.Object [] arrObj = assetsInfo.assetsBundle.LoadAllAssets();
  99. foreach(UnityEngine.Object obj in arrObj)
  100. {
  101. CDebug.Log(obj.name + " " + obj.ToString());
  102. }
  103. }
  104. }
  105. /// <summary>添加资源</summary>
  106. public void AddAssets(string assetsName, bool isDebug = true)
  107. {
  108. if (string.IsNullOrEmpty (assetsName))
  109. {
  110. return;
  111. }
  112. for (int i = 0; i < listAssetsName.Count; i++)
  113. {
  114. if (listAssetsName[i] == assetsName)
  115. {
  116. #if UNITY_EDITOR
  117. if (isDebug)
  118. {
  119. CDebug.LogError("需要下载的资源已经存在:" + assetsName);
  120. }
  121. #endif
  122. return;
  123. }
  124. }
  125. listAssetsName.Add(assetsName);
  126. //有新资源加入,需要重新获取资源配置
  127. assetsConfigList.Clear();
  128. }
  129. /// <summary>释放函数</summary>
  130. public void Dispose()
  131. {
  132. DisposeDelegate();
  133. listAssetsName = null;
  134. int forLeng = listAssetsSource.Count;
  135. for (int i = 0; i < forLeng; i++)
  136. {
  137. DisposeAssets(listAssetsSource[i]);
  138. }
  139. listAssetsSource.Clear();
  140. isDispose = true;
  141. }
  142. /// <summary>释放下载委托</summary>
  143. public void DisposeDelegate()
  144. {
  145. onComplete = null;
  146. onProgress = null;
  147. objectParam = null;
  148. }
  149. /// <summary>释放一个资源</summary>
  150. private void DisposeAssets(CAssetsInfo assetsInfo)
  151. {
  152. //资源本身的使用计数
  153. assetsInfo.DelUseCount();
  154. //资源依赖项的实用技术
  155. for (int i = 0; i < assetsInfo.depAssetsList.Count; i++)
  156. {
  157. DisposeAssets(assetsInfo.depAssetsList[i]);
  158. }
  159. }
  160. /// <summary>修改下载进度</summary>
  161. public void UpdateProgress(string assetsName, int nProgress)
  162. {
  163. if (assetsType == AssetsType.Config)
  164. {
  165. return;
  166. }
  167. //这里可以根据配置文件中的size 和 CLoaderAB.LoadProgress 计算下载进度、下载速度等数值。
  168. this.NewDictProgress();
  169. //修改进度
  170. if (DictProgress.ContainsKey(assetsName))
  171. {
  172. DictProgress[assetsName] = nProgress;
  173. }
  174. if(onProgress!=null)
  175. {
  176. onProgress(this);
  177. }
  178. }
  179. /// <summary>获得下载进度</summary>
  180. public int LoadProgress
  181. {
  182. get
  183. {
  184. if (assetsType == AssetsType.Config)
  185. {
  186. return 0;
  187. }
  188. float loadProgress = 0;
  189. float assetsScale = 1.0f / DictProgress.Count;
  190. foreach(int progress in DictProgress.Values)
  191. {
  192. loadProgress += (float)progress * assetsScale;
  193. }
  194. return (int)loadProgress;
  195. }
  196. }
  197. /// <summary>实例化下载进度数据</summary>
  198. private void NewDictProgress()
  199. {
  200. if(this.listAssetsName==null)
  201. {
  202. return;
  203. }
  204. if (assetsType == AssetsType.Config)
  205. {
  206. return;
  207. }
  208. if (DictProgress != null)
  209. {
  210. return;
  211. }
  212. DictProgress = new Dictionary<string, int>();
  213. //获得需要下载的全部资源
  214. int forLeng = this.listAssetsName.Count;
  215. for (int i = 0; i < forLeng; i++)
  216. {
  217. this.AddDictProgress(listAssetsName[i]);
  218. }
  219. }
  220. /// <summary>添加需要计算进度的资源</summary>
  221. private void AddDictProgress(string assestName)
  222. {
  223. if(!DictProgress.ContainsKey(assestName))
  224. {
  225. DictProgress.Add(assestName, 0);
  226. }
  227. CAssetsConfigInfo assetsConfigInfo = CLoaderConfig.GetInstance().GetAssetsConfig(assestName);
  228. int forLeng = assetsConfigInfo.assetsDepNameList.Count;
  229. for (int i = 0; i < forLeng; i++)
  230. {
  231. AddDictProgress(assetsConfigInfo.assetsDepNameList[i]);
  232. }
  233. }
  234. /// <summary>资源下载完毕</summary>
  235. public void OnCompleteCallBack()
  236. {
  237. //如果已经被释放,就在释放一次
  238. if(isDispose)
  239. {
  240. Dispose();
  241. }
  242. if (onComplete != null)
  243. {
  244. onComplete(this);
  245. }
  246. }
  247. /// <summary>数据源对象 临时的</summary>
  248. private Dictionary<string, CAssetsInfo> dictAssetsSource = new Dictionary<string, CAssetsInfo>();
  249. /// <summary>检查资源是否下载完毕</summary>
  250. internal bool ChectLoadComplete(CAssetsInfo assetsInfo)
  251. {
  252. //添加到列表中
  253. if (!dictAssetsSource.ContainsKey(assetsInfo.assetsName))
  254. {
  255. dictAssetsSource.Add(assetsInfo.assetsName, assetsInfo);
  256. }
  257. else
  258. {
  259. #if UNITY_EDITOR
  260. CDebug.LogWarning("有资源的下载完成逻辑执行重复: assetsName=" + assetsInfo.assetsName);
  261. #endif
  262. }
  263. int forLeng = 0;
  264. if (assetsType != AssetsType.Config)
  265. {
  266. forLeng = this.AssetsConfigList.Count;
  267. for (int i = 0; i < forLeng; i++)
  268. {
  269. if (!this.CheckDepLoadComplete(AssetsConfigList[i], listAssetsSource))
  270. {
  271. return false;
  272. }
  273. }
  274. }
  275. else
  276. {
  277. forLeng = this.listAssetsName.Count;
  278. for (int i = 0; i < forLeng; i++)
  279. {
  280. //资源本身下载完毕
  281. if (dictAssetsSource.ContainsKey(listAssetsName[i]))
  282. {
  283. AssetsAddGoList(listAssetsSource, dictAssetsSource[listAssetsName[i]]);
  284. }
  285. else
  286. {
  287. return false;
  288. }
  289. }
  290. }
  291. //清理临时数据引用
  292. dictAssetsSource.Clear();
  293. return true;
  294. }
  295. /// <summary>检查依赖的资源是否下载完毕</summary>
  296. private bool CheckDepLoadComplete(CAssetsConfigInfo configInfo, List<CAssetsInfo> depAssetList)
  297. {
  298. if (configInfo==null)
  299. {
  300. #if UNITY_EDITOR
  301. CDebug.LogError("有检查资源是否现在完成时发生错误");
  302. #endif
  303. return true;
  304. }
  305. //资源本身下载完毕
  306. if(dictAssetsSource.ContainsKey(configInfo.assetsName))
  307. {
  308. AssetsAddGoList(depAssetList, dictAssetsSource[configInfo.assetsName]);
  309. }
  310. else
  311. {
  312. return false;
  313. }
  314. int depCount = configInfo.AssetsDepInfoList.Count;
  315. bool isComplete = true;
  316. for (int i = 0; i < depCount; i++)
  317. {
  318. isComplete = CheckDepLoadComplete(configInfo.AssetsDepInfoList[i], dictAssetsSource[configInfo.assetsName].depAssetsList);
  319. //有一个依赖的资源未下载完成就认为下载失败
  320. if(!isComplete)
  321. {
  322. return false;
  323. }
  324. }
  325. return isComplete;
  326. }
  327. /// <summary>把资源对象添加到资源对象列表中</summary>
  328. private void AssetsAddGoList(List<CAssetsInfo> depAssetList, CAssetsInfo assetsInfo)
  329. {
  330. if (depAssetList.IndexOf(assetsInfo) == -1)
  331. {
  332. depAssetList.Add(assetsInfo);
  333. }
  334. }
  335. }
  336. }