| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using System.Collections;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- namespace Assets.Plugins.CommonTool
- {
- public class AsyncImageDownload : MonoBehaviour
- {
- public Sprite placeholder;
- private static AsyncImageDownload _instance;
- //public static AsyncImageDownload GetInstance() { return Instance; }
- public static AsyncImageDownload Instance
- {
- get
- {
- if (_instance == null)
- {
- GameObject obj = new GameObject("AsyncImageDownloadObj");
- _instance = obj.AddComponent<AsyncImageDownload>();
- DontDestroyOnLoad(obj);
- _instance.Init();
- }
- return _instance;
- }
- }
- public bool Init()
- {
- if (!Directory.Exists(Application.persistentDataPath + "/ImageCache/"))
- {
- Directory.CreateDirectory(Application.persistentDataPath + "/ImageCache/");
- }
- if (placeholder == null)
- {
- placeholder = Resources.Load("placeholder") as Sprite;
- }
- return true;
- }
- public void SetAsyncImage(string url, Image image)
- {
- //开始下载图片前,将UITexture的主图片设置为占位图
- image.sprite = placeholder;
- //判断是否是第一次加载这张图片
- //StartCoroutine(!File.Exists(path + url.GetHashCode()) ? DownloadImage(url, image) : LoadLocalImage(url, image));
- StartCoroutine(DownloadImage(url, image));
- }
- public void SetAsyncImage(string url,GameObject go)
- {
- StartCoroutine(LoadTextureFromInternet(url, go));
- }
- //IEnumerator DownloadImage(string url, Image image)
- //{
- // Debug.Log("downloading new image:" + path + url.GetHashCode());//url转换HD5作为名字
- // var www = new WWW(url);
- // yield return www;
- // Texture2D tex2d = www.texture;
- // //将图片保存至缓存路径
- // byte[] pngData = tex2d.EncodeToPNG();
- // File.WriteAllBytes(path + url.GetHashCode(), pngData);
- // Sprite m_sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));
- // image.sprite = m_sprite;
- //}
- IEnumerator DownloadImage( string url,Image imageComp)
- {
- //using UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url);
- //yield return uwr.SendWebRequest();
- //if (uwr.result ==UnityWebRequest.Result.ProtocolError)
- // Debug.Log(uwr.error);
- //else
- //{
- // if (uwr.isDone)
- // {
- // //Texture2D texture2d = new Texture2D(width, height);
- // var texture2d = DownloadHandlerTexture.GetContent(uwr);
- // int width = texture2d.width; //1920;
- // int height = texture2d.height; //1080;
- // byte[] pngData = texture2d.EncodeToPNG();
- // File.WriteAllBytes(path + url.GetHashCode(), pngData);
- // Sprite tempSprite = Sprite.Create(texture2d, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
- // imageComp.sprite = tempSprite;
- // Resources.UnloadUnusedAssets();
- // }
- //}
- UnityWebRequest request = new UnityWebRequest(url);
- DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
- request.downloadHandler = texture;
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.ProtocolError)
- {
- Debug.Log(request.error);
- }
- var pic = texture.texture;
- Sprite sp = Sprite.Create((Texture2D)pic, new Rect(0, 0, pic.width, pic.height), Vector2.zero);
- imageComp.sprite = sp;
- }
-
- IEnumerator LoadLocalImage(string url, Image imageComp)
- {
- string filePath = "file:///" + path + url.GetHashCode();
- Debug.Log("getting local image:" + filePath);
- using UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url);
- yield return uwr.SendWebRequest();
- //WWW www = new WWW(filePath);
- //yield return www;
- if (uwr.result == UnityWebRequest.Result.ProtocolError)
- Debug.Log(uwr.error);
- else
- {
- if (uwr.isDone)
- {
- //Texture2D texture2d = new Texture2D(width, height);
- var texture2d = DownloadHandlerTexture.GetContent(uwr);
- int width = texture2d.width; //1920;
- int height = texture2d.height; //1080;
- Sprite tempSprite = Sprite.Create(texture2d, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
- imageComp.sprite = tempSprite;
- Resources.UnloadUnusedAssets();
- }
- }
- }
- IEnumerator LoadTextureFromInternet(string url,GameObject go)
- {
- UnityWebRequest request = new UnityWebRequest(url);
- DownloadHandlerTexture texture = new DownloadHandlerTexture(true);
- request.downloadHandler = texture;
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.ProtocolError)
- {
- Debug.Log(request.error);
- }
- var pic = texture.texture;
- Image tempImage = go.GetComponent<Image>();
- if (tempImage != null)
- {
- Sprite sp = Sprite.Create((Texture2D)pic, new Rect(0, 0, pic.width, pic.height), Vector2.zero);
- tempImage.sprite = sp;
- go.GetComponent<MeshRenderer>().material.mainTexture = pic;
- }
- else
- {
- Debug.Log("物体上添加【IMAGE】组件");
- }
- }
-
- public string path => Application.persistentDataPath + "/ImageCache/"; //pc,ios //android :jar:file//
- }
- }
|