TextureLoadHelp.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. using UnityEngine.UI;
  9. public class TextureLoadHelp : MonoBehaviour
  10. {
  11. public static TextureLoadHelp _Instance;
  12. private Dictionary<string, TempTexData> tempTexList;
  13. private Dictionary<long, Material> waitLoadImgActionList;
  14. private Dictionary<long, RawImage> waitLoadImgActionList_RawImg;
  15. public float currentTotalSize = 0;
  16. public float maxSize = 500; //图片缓存-m
  17. private Dictionary<string, AssetBundle> loadDoneABList;
  18. private Dictionary<string, int> loadingABList;
  19. public Texture map_def;
  20. //临时存的封面图
  21. public Texture[] tempTextures;
  22. public string[] tempTextureName;
  23. public Texture GetTempTexture(string name)
  24. {
  25. int index = -1;
  26. for (int i = 0; i < tempTextureName.Length; i++)
  27. {
  28. if (tempTextureName[i].Equals(name))
  29. {
  30. index = i;
  31. break;
  32. }
  33. }
  34. if (index != -1)
  35. {
  36. return tempTextures[index];
  37. }
  38. else
  39. {
  40. return null;
  41. }
  42. }
  43. private void Awake()
  44. {
  45. _Instance = this;
  46. tempTexList = new Dictionary<string, TempTexData>();
  47. waitLoadImgActionList = new Dictionary<long, Material>();
  48. waitLoadImgActionList_RawImg = new Dictionary<long, RawImage>();
  49. loadDoneABList = new Dictionary<string, AssetBundle>();
  50. loadingABList = new Dictionary<string, int>();
  51. }
  52. /// <summary>
  53. /// 加载图片
  54. /// </summary>
  55. /// <param name="path"></param>
  56. /// <param name="rawImg"></param>
  57. /// <returns></returns>
  58. public long LoadTexFromUrl(string path, RawImage rawImg)
  59. {
  60. long loadId = GetDownId();
  61. //Debug.Log("下载图片:"+loadId);
  62. // if (CheckCache(path))
  63. // {
  64. // rawImg.texture = LoadFromCache(path);
  65. // return 0;
  66. // }
  67. if (tempTexList.TryGetValue(path, out var value))
  68. {
  69. value.isUsing = true;
  70. rawImg.texture = value.tex;
  71. loadId = 0;
  72. }
  73. else
  74. {
  75. waitLoadImgActionList_RawImg.Add(loadId, rawImg);
  76. StartCoroutine(DownLoadTex_Raw(path, loadId));
  77. }
  78. return loadId;
  79. }
  80. public long LoadTexFromUrl_AB(string ab_Name, string FileName, Material mat)
  81. {
  82. if (tempTexList.TryGetValue(FileName, out var value))
  83. {
  84. value.isUsing = true;
  85. mat.mainTexture = value.tex;
  86. return 0;
  87. }
  88. mat.mainTexture = map_def;
  89. long loadId = GetDownId();
  90. waitLoadImgActionList.Add(loadId, mat);
  91. StartCoroutine(DownLoadTex_AB(ab_Name, FileName, loadId));
  92. return loadId;
  93. }
  94. /// <summary>
  95. /// 取消加载
  96. /// </summary>
  97. /// <param name="loadId"></param>
  98. public void CancelLoad(long loadId)
  99. {
  100. if (waitLoadImgActionList.ContainsKey(loadId))
  101. {
  102. waitLoadImgActionList.Remove(loadId);
  103. }
  104. }
  105. /// <summary>
  106. /// 标记缓存图片为未使用
  107. /// </summary>
  108. /// <param name="path"></param>
  109. public void UnUsingTex(string path)
  110. {
  111. if (tempTexList.TryGetValue(path, out TempTexData disposeData))
  112. {
  113. disposeData.isUsing = false;
  114. }
  115. else
  116. {
  117. Debug.Log($"不存在缓存:{path}");
  118. }
  119. }
  120. IEnumerator DownLoadTex_AB(string ab_Name, string fileName, long loadId)
  121. {
  122. if (loadingABList.ContainsKey(ab_Name))
  123. {
  124. //Debug.Log($"{ab_Name} downloading...wait");
  125. yield return new WaitUntil(() => loadingABList[ab_Name] > 0);
  126. // if (loadingABList.ContainsKey(ab_Name))
  127. // {
  128. // loadingABList.Remove(ab_Name);
  129. // }
  130. }
  131. AssetBundle tempAb;
  132. Texture2D tex;
  133. if (loadDoneABList.TryGetValue(ab_Name, out var value))
  134. {
  135. tempAb = value;
  136. tex = tempAb.LoadAsset<Texture2D>(fileName);
  137. if (waitLoadImgActionList.TryGetValue(loadId, out Material targetMat))
  138. {
  139. if (targetMat != null && tex != null)
  140. {
  141. targetMat.mainTexture = tex;
  142. }
  143. else
  144. {
  145. if (targetMat == null)
  146. {
  147. Debug.Log($"{fileName} 材质球不存在");
  148. }
  149. if (tex == null)
  150. {
  151. Debug.Log($"{fileName} 图片不存在");
  152. }
  153. }
  154. waitLoadImgActionList.Remove(loadId);
  155. }
  156. }
  157. else
  158. {
  159. //添加到正在下载
  160. loadingABList.TryAdd(ab_Name, 0);
  161. #if UNITY_EDITOR
  162. WWW www = new WWW($"{Application.streamingAssetsPath}/{ab_Name}");
  163. #else
  164. WWW www = WWW.LoadFromCacheOrDownload($"{Application.streamingAssetsPath}/{ab_Name}",0);
  165. #endif
  166. yield return www;
  167. if (www.isDone)
  168. {
  169. tempAb = www.assetBundle;
  170. loadDoneABList.Add(ab_Name, tempAb);
  171. loadingABList[ab_Name] = 1;
  172. tex = tempAb.LoadAsset<Texture2D>(fileName);
  173. tempTexList.TryAdd(fileName, new TempTexData()
  174. {
  175. loadTime = GetTimeStamp(),
  176. name = fileName,
  177. tex = tex,
  178. size = 0,
  179. isUsing = true
  180. });
  181. if (waitLoadImgActionList.TryGetValue(loadId, out Material targetMat))
  182. {
  183. if (targetMat != null && tex != null)
  184. {
  185. targetMat.mainTexture = tex;
  186. }
  187. else
  188. {
  189. if (targetMat == null)
  190. {
  191. Debug.Log($"{fileName} 材质球不存在");
  192. }
  193. if (tex == null)
  194. {
  195. Debug.Log($"{fileName} 图片不存在");
  196. }
  197. }
  198. waitLoadImgActionList.Remove(loadId);
  199. }
  200. }
  201. else
  202. {
  203. loadingABList[ab_Name] = 2;
  204. Debug.LogError($"{ab_Name}下载失败:{www.error}");
  205. }
  206. www.Dispose();
  207. }
  208. }
  209. IEnumerator DownLoadTex(string path, long loadId)
  210. {
  211. UnityWebRequest www = UnityWebRequestTexture.GetTexture(path);
  212. yield return www.SendWebRequest();
  213. if (www.downloadHandler.isDone)
  214. {
  215. if (waitLoadImgActionList.ContainsKey(loadId))
  216. {
  217. var texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
  218. float tempSize = GetSize_m(www.downloadedBytes);
  219. tempTexList.TryAdd(path, new TempTexData()
  220. {
  221. loadTime = GetTimeStamp(),
  222. name = path,
  223. tex = texture,
  224. size = tempSize,
  225. isUsing = true
  226. });
  227. currentTotalSize += tempSize;
  228. CheckSize();
  229. if (waitLoadImgActionList.TryGetValue(loadId, out Material targetMat))
  230. {
  231. if (targetMat != null)
  232. {
  233. targetMat.mainTexture = texture;
  234. }
  235. waitLoadImgActionList.Remove(loadId);
  236. }
  237. SaveInCache(path, www.downloadHandler.data);
  238. }
  239. else
  240. {
  241. www.disposeDownloadHandlerOnDispose = true;
  242. Debug.Log(path + " 下载已取消");
  243. }
  244. }
  245. else
  246. {
  247. Debug.LogError($"{path}下载失败:{www.downloadHandler.error}");
  248. }
  249. www.Dispose();
  250. }
  251. IEnumerator DownLoadTex_Raw(string path, long loadId)
  252. {
  253. #if UNITY_EDITOR
  254. WWW www = new WWW(path);
  255. #else
  256. WWW www = WWW.LoadFromCacheOrDownload($"{Application.streamingAssetsPath}/{ab_Name}",0);
  257. #endif
  258. yield return www;
  259. if (www.isDone)
  260. {
  261. if (waitLoadImgActionList_RawImg.ContainsKey(loadId))
  262. {
  263. var texture = www.texture;
  264. float tempSize = GetSize_m((ulong)www.bytesDownloaded);
  265. tempTexList.TryAdd(path, new TempTexData()
  266. {
  267. loadTime = GetTimeStamp(),
  268. name = path,
  269. tex = texture,
  270. size = tempSize,
  271. isUsing = true
  272. });
  273. currentTotalSize += tempSize;
  274. CheckSize();
  275. if (waitLoadImgActionList_RawImg.TryGetValue(loadId, out RawImage rawImage))
  276. {
  277. if (rawImage != null)
  278. {
  279. rawImage.texture = texture;
  280. }
  281. waitLoadImgActionList_RawImg.Remove(loadId);
  282. }
  283. }
  284. else
  285. {
  286. Debug.Log(path + " 下载已取消");
  287. }
  288. }
  289. else
  290. {
  291. Debug.LogError($"{path}下载失败:{www.error}");
  292. }
  293. www.Dispose();
  294. }
  295. private void SaveInCache(string url, byte[] data)
  296. {
  297. string fileName = Path.GetFileName(url);
  298. string path = Application.persistentDataPath;
  299. File.WriteAllBytes(path + fileName, data);
  300. Debug.Log($"缓存:{path}{fileName}");
  301. }
  302. private Texture2D LoadFromCache(string url)
  303. {
  304. string fileName = Path.GetFileName(url);
  305. string path = Application.persistentDataPath;
  306. byte[] texBytes = File.ReadAllBytes(path + fileName);
  307. currentTotalSize += GetSize_m((ulong)texBytes.Length);
  308. Texture2D tempTex = new Texture2D(2, 2);
  309. tempTex.LoadImage(texBytes);
  310. tempTex.Apply();
  311. return tempTex;
  312. }
  313. // private bool CheckCache(string url)
  314. // {
  315. // string fileName = Path.GetFileName(url);
  316. // string path = Application.persistentDataPath;
  317. // //Debug.Log($"CaChe:{path}");
  318. // return File.Exists(path + fileName);
  319. // }
  320. public void CheckSize()
  321. {
  322. if (currentTotalSize >= maxSize)
  323. {
  324. List<TempTexData> values = tempTexList.Values.ToList();
  325. values.Sort();
  326. for (int i = 0; i < values.Count; i++)
  327. {
  328. if (!values[i].isUsing)
  329. {
  330. if (tempTexList.TryGetValue(values[i].name, out TempTexData disposeData))
  331. {
  332. disposeData.tex = null;
  333. currentTotalSize -= disposeData.size;
  334. tempTexList.Remove(disposeData.name);
  335. }
  336. }
  337. if (currentTotalSize < maxSize)
  338. {
  339. break;
  340. }
  341. }
  342. if (currentTotalSize >= maxSize)
  343. {
  344. Debug.Log($"临时图片已超过限制大小且无可释放项!!!:{currentTotalSize}/{maxSize}");
  345. }
  346. }
  347. }
  348. /// <summary>
  349. /// 清空全部缓存
  350. /// </summary>
  351. public void ClearAllTempTex()
  352. {
  353. List<TempTexData> values = tempTexList.Values.ToList();
  354. for (int i = 0; i < values.Count; i++)
  355. {
  356. if (tempTexList.TryGetValue(values[i].name, out TempTexData disposeData))
  357. {
  358. disposeData.tex = null;
  359. currentTotalSize -= disposeData.size;
  360. tempTexList.Remove(disposeData.name);
  361. }
  362. }
  363. waitLoadImgActionList.Clear();
  364. tempTexList.Clear();
  365. Resources.UnloadUnusedAssets();
  366. GC.Collect();
  367. }
  368. private long GetDownId()
  369. {
  370. long id = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  371. while (waitLoadImgActionList.ContainsKey(id))
  372. {
  373. id++;
  374. }
  375. return id;
  376. }
  377. private long GetTimeStamp()
  378. {
  379. return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  380. }
  381. private float GetSize_m(ulong size_b)
  382. {
  383. return (size_b * 1.0f / (1024.0f * 1024.0f));
  384. }
  385. }
  386. public class TempTexData : IComparable<TempTexData>
  387. {
  388. public string name;
  389. public Texture tex;
  390. public long loadTime;
  391. public float size;
  392. public bool isUsing = true;
  393. public int CompareTo(TempTexData other)
  394. {
  395. if (other.loadTime < loadTime)
  396. {
  397. return 1;
  398. }
  399. if (other.loadTime == loadTime)
  400. {
  401. return 0;
  402. }
  403. return -1;
  404. }
  405. }