AsyncImageDownload.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Collections;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. namespace Assets.Plugins.CommonTool
  7. {
  8. public class AsyncImageDownload : MonoBehaviour
  9. {
  10. public Sprite placeholder;
  11. private static AsyncImageDownload _instance;
  12. //public static AsyncImageDownload GetInstance() { return Instance; }
  13. public static AsyncImageDownload Instance
  14. {
  15. get
  16. {
  17. if (_instance == null)
  18. {
  19. GameObject obj = new GameObject("AsyncImageDownloadObj");
  20. _instance = obj.AddComponent<AsyncImageDownload>();
  21. DontDestroyOnLoad(obj);
  22. _instance.Init();
  23. }
  24. return _instance;
  25. }
  26. }
  27. public bool Init()
  28. {
  29. if (!Directory.Exists(Application.persistentDataPath + "/ImageCache/"))
  30. {
  31. Directory.CreateDirectory(Application.persistentDataPath + "/ImageCache/");
  32. }
  33. if (placeholder == null)
  34. {
  35. placeholder = Resources.Load("placeholder") as Sprite;
  36. }
  37. return true;
  38. }
  39. public void SetAsyncImage(string url, Image image)
  40. {
  41. //开始下载图片前,将UITexture的主图片设置为占位图
  42. image.sprite = placeholder;
  43. //判断是否是第一次加载这张图片
  44. //StartCoroutine(!File.Exists(path + url.GetHashCode()) ? DownloadImage(url, image) : LoadLocalImage(url, image));
  45. StartCoroutine(DownloadImage(url, image));
  46. }
  47. public void SetAsyncImage(string url,GameObject go)
  48. {
  49. StartCoroutine(LoadTextureFromInternet(url, go));
  50. }
  51. //IEnumerator DownloadImage(string url, Image image)
  52. //{
  53. // Debug.Log("downloading new image:" + path + url.GetHashCode());//url转换HD5作为名字
  54. // var www = new WWW(url);
  55. // yield return www;
  56. // Texture2D tex2d = www.texture;
  57. // //将图片保存至缓存路径
  58. // byte[] pngData = tex2d.EncodeToPNG();
  59. // File.WriteAllBytes(path + url.GetHashCode(), pngData);
  60. // Sprite m_sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));
  61. // image.sprite = m_sprite;
  62. //}
  63. IEnumerator DownloadImage( string url,Image imageComp)
  64. {
  65. //using UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url);
  66. //yield return uwr.SendWebRequest();
  67. //if (uwr.result ==UnityWebRequest.Result.ProtocolError)
  68. // Debug.Log(uwr.error);
  69. //else
  70. //{
  71. // if (uwr.isDone)
  72. // {
  73. // //Texture2D texture2d = new Texture2D(width, height);
  74. // var texture2d = DownloadHandlerTexture.GetContent(uwr);
  75. // int width = texture2d.width; //1920;
  76. // int height = texture2d.height; //1080;
  77. // byte[] pngData = texture2d.EncodeToPNG();
  78. // File.WriteAllBytes(path + url.GetHashCode(), pngData);
  79. // Sprite tempSprite = Sprite.Create(texture2d, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
  80. // imageComp.sprite = tempSprite;
  81. // Resources.UnloadUnusedAssets();
  82. // }
  83. //}
  84. UnityWebRequest request = new UnityWebRequest(url);
  85. DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
  86. request.downloadHandler = texture;
  87. yield return request.SendWebRequest();
  88. if (request.result == UnityWebRequest.Result.ProtocolError)
  89. {
  90. Debug.Log(request.error);
  91. }
  92. var pic = texture.texture;
  93. Sprite sp = Sprite.Create((Texture2D)pic, new Rect(0, 0, pic.width, pic.height), Vector2.zero);
  94. imageComp.sprite = sp;
  95. }
  96. IEnumerator LoadLocalImage(string url, Image imageComp)
  97. {
  98. string filePath = "file:///" + path + url.GetHashCode();
  99. Debug.Log("getting local image:" + filePath);
  100. using UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url);
  101. yield return uwr.SendWebRequest();
  102. //WWW www = new WWW(filePath);
  103. //yield return www;
  104. if (uwr.result == UnityWebRequest.Result.ProtocolError)
  105. Debug.Log(uwr.error);
  106. else
  107. {
  108. if (uwr.isDone)
  109. {
  110. //Texture2D texture2d = new Texture2D(width, height);
  111. var texture2d = DownloadHandlerTexture.GetContent(uwr);
  112. int width = texture2d.width; //1920;
  113. int height = texture2d.height; //1080;
  114. Sprite tempSprite = Sprite.Create(texture2d, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
  115. imageComp.sprite = tempSprite;
  116. Resources.UnloadUnusedAssets();
  117. }
  118. }
  119. }
  120. IEnumerator LoadTextureFromInternet(string url,GameObject go)
  121. {
  122. UnityWebRequest request = new UnityWebRequest(url);
  123. DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
  124. request.downloadHandler = texture;
  125. yield return request.SendWebRequest();
  126. if (request.result == UnityWebRequest.Result.ProtocolError)
  127. {
  128. Debug.Log(request.error);
  129. }
  130. var pic = texture.texture;
  131. Image tempImage = go.GetComponent<Image>();
  132. if (tempImage != null)
  133. {
  134. Sprite sp = Sprite.Create((Texture2D)pic, new Rect(0, 0, pic.width, pic.height), Vector2.zero);
  135. tempImage.sprite = sp;
  136. go.GetComponent<MeshRenderer>().material.mainTexture = pic;
  137. }
  138. else
  139. {
  140. Debug.Log("物体上添加【IMAGE】组件");
  141. }
  142. }
  143. public string path => Application.persistentDataPath + "/ImageCache/"; //pc,ios //android :jar:file//
  144. }
  145. }