unity显示网络图片

最近的项目中遇到这个需求,要将json中提供的大量图片url下载并显示,由此要封装一个初次加载时进行下载后续直接读本地路径并且下载过程中图片处不能空白的一个工具类。

转载自:https://blog.csdn.net/yupu56/article/details/50915306

IOS工程师都应该用过SDWebImage或者android工程师应该使用过Glide,fresco,Imageloader等,在Unity里面,我按照同样的原理封装了一个ImageLoader 。

原理就是先用一张placeholder来显示图片,等待图片加载,等加载完了之后替换placeholder,第二次加载网络图片时,先判断本地时候已经加载过图片,如果加载过就从本地获取图片,如果没有就去网络上加载。

下面代码:

 
  1. using UnityEngine;

  2. using UnityEngine.UI;

  3. using System.Collections;

  4. using System.IO;

  5.  
  6. public class AsyncImageDownload : MonoBehaviour

  7. {

  8. public Sprite placeholder;

  9.  
  10. private static AsyncImageDownload _instance = null;

  11. public static AsyncImageDownload GetInstance() { return Instance; }

  12. public static AsyncImageDownload Instance

  13. {

  14. get

  15. {

  16. if(_instance==null)

  17. {

  18. GameObject obj = new GameObject("AsyncImageDownload");

  19. _instance = obj.AddComponent<AsyncImageDownload>();

  20. DontDestroyOnLoad(obj);

  21. _instance.Init();

  22. }

  23. return _instance;

  24. }

  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. }

  40.  
  41. public void SetAsyncImage(string url, Image image)

  42. {

  43. //开始下载图片前,将UITexture的主图片设置为占位图

  44. image.sprite = placeholder;

  45.  
  46. //判断是否是第一次加载这张图片

  47. if (!File.Exists(path + url.GetHashCode()))

  48. {

  49. //如果之前不存在缓存文件

  50. StartCoroutine(DownloadImage(url, image));

  51. }

  52. else {

  53. StartCoroutine(LoadLocalImage(url, image));

  54. }

  55. }

  56.  
  57. IEnumerator DownloadImage(string url, Image image)

  58. {

  59. Debug.Log("downloading new image:" + path + url.GetHashCode());//url转换HD5作为名字

  60. WWW www = new WWW(url);

  61. yield return www;

  62.  
  63. Texture2D tex2d = www.texture;

  64. //将图片保存至缓存路径

  65. byte[] pngData = tex2d.EncodeToPNG();

  66. File.WriteAllBytes(path + url.GetHashCode(), pngData);

  67.  
  68. Sprite m_sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));

  69. image.sprite = m_sprite;

  70. }

  71.  
  72. IEnumerator LoadLocalImage(string url, Image image)

  73. {

  74. string filePath = "file:///" + path + url.GetHashCode();

  75.  
  76. Debug.Log("getting local image:" + filePath);

  77. WWW www = new WWW(filePath);

  78. yield return www;

  79.  
  80. Texture2D texture = www.texture;

  81. Sprite m_sprite = Sprite.Create(texture,new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));

  82. image.sprite = m_sprite;

  83. }

  84.  
  85. public string path

  86. {

  87. get

  88. {

  89. //pc,ios //android :jar:file//

  90. return Application.persistentDataPath + "/ImageCache/";

  91.  
  92. }

  93. }

  94. }

End

猜你喜欢

转载自blog.csdn.net/weixin_39568744/article/details/82019088