TextureLoadHelp.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. WWW www = new WWW(path);
  254. yield return www;
  255. if (www.isDone)
  256. {
  257. if (waitLoadImgActionList_RawImg.ContainsKey(loadId))
  258. {
  259. var texture = www.texture;
  260. float tempSize = GetSize_m((ulong)www.bytesDownloaded);
  261. tempTexList.TryAdd(path, new TempTexData()
  262. {
  263. loadTime = GetTimeStamp(),
  264. name = path,
  265. tex = texture,
  266. size = tempSize,
  267. isUsing = true
  268. });
  269. currentTotalSize += tempSize;
  270. CheckSize();
  271. if (waitLoadImgActionList_RawImg.TryGetValue(loadId, out RawImage rawImage))
  272. {
  273. if (rawImage != null)
  274. {
  275. rawImage.texture = texture;
  276. }
  277. waitLoadImgActionList_RawImg.Remove(loadId);
  278. }
  279. }
  280. else
  281. {
  282. Debug.Log(path + " 下载已取消");
  283. }
  284. }
  285. else
  286. {
  287. Debug.LogError($"{path}下载失败:{www.error}");
  288. }
  289. www.Dispose();
  290. }
  291. private void SaveInCache(string url, byte[] data)
  292. {
  293. string fileName = Path.GetFileName(url);
  294. string path = Application.persistentDataPath;
  295. File.WriteAllBytes(path + fileName, data);
  296. Debug.Log($"缓存:{path}{fileName}");
  297. }
  298. private Texture2D LoadFromCache(string url)
  299. {
  300. string fileName = Path.GetFileName(url);
  301. string path = Application.persistentDataPath;
  302. byte[] texBytes = File.ReadAllBytes(path + fileName);
  303. currentTotalSize += GetSize_m((ulong)texBytes.Length);
  304. Texture2D tempTex = new Texture2D(2, 2);
  305. tempTex.LoadImage(texBytes);
  306. tempTex.Apply();
  307. return tempTex;
  308. }
  309. // private bool CheckCache(string url)
  310. // {
  311. // string fileName = Path.GetFileName(url);
  312. // string path = Application.persistentDataPath;
  313. // //Debug.Log($"CaChe:{path}");
  314. // return File.Exists(path + fileName);
  315. // }
  316. public void CheckSize()
  317. {
  318. if (currentTotalSize >= maxSize)
  319. {
  320. List<TempTexData> values = tempTexList.Values.ToList();
  321. values.Sort();
  322. for (int i = 0; i < values.Count; i++)
  323. {
  324. if (!values[i].isUsing)
  325. {
  326. if (tempTexList.TryGetValue(values[i].name, out TempTexData disposeData))
  327. {
  328. disposeData.tex = null;
  329. currentTotalSize -= disposeData.size;
  330. tempTexList.Remove(disposeData.name);
  331. }
  332. }
  333. if (currentTotalSize < maxSize)
  334. {
  335. break;
  336. }
  337. }
  338. if (currentTotalSize >= maxSize)
  339. {
  340. Debug.Log($"临时图片已超过限制大小且无可释放项!!!:{currentTotalSize}/{maxSize}");
  341. }
  342. }
  343. }
  344. /// <summary>
  345. /// 清空全部缓存
  346. /// </summary>
  347. public void ClearAllTempTex()
  348. {
  349. List<TempTexData> values = tempTexList.Values.ToList();
  350. for (int i = 0; i < values.Count; i++)
  351. {
  352. if (tempTexList.TryGetValue(values[i].name, out TempTexData disposeData))
  353. {
  354. disposeData.tex = null;
  355. currentTotalSize -= disposeData.size;
  356. tempTexList.Remove(disposeData.name);
  357. }
  358. }
  359. waitLoadImgActionList.Clear();
  360. tempTexList.Clear();
  361. Resources.UnloadUnusedAssets();
  362. GC.Collect();
  363. }
  364. private long GetDownId()
  365. {
  366. long id = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  367. while (waitLoadImgActionList.ContainsKey(id))
  368. {
  369. id++;
  370. }
  371. return id;
  372. }
  373. private long GetTimeStamp()
  374. {
  375. return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  376. }
  377. private float GetSize_m(ulong size_b)
  378. {
  379. return (size_b * 1.0f / (1024.0f * 1024.0f));
  380. }
  381. }
  382. public class TempTexData : IComparable<TempTexData>
  383. {
  384. public string name;
  385. public Texture tex;
  386. public long loadTime;
  387. public float size;
  388. public bool isUsing = true;
  389. public int CompareTo(TempTexData other)
  390. {
  391. if (other.loadTime < loadTime)
  392. {
  393. return 1;
  394. }
  395. if (other.loadTime == loadTime)
  396. {
  397. return 0;
  398. }
  399. return -1;
  400. }
  401. }