using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using System; using System.IO; using UnityEngine.UI; public class WebImage { public string identifyId; public virtual void SetImage(Texture2D texture) { } } public class WebRawImage: WebImage { public RawImage mRawImage; public override void SetImage(Texture2D texture) { if (texture == null) { mRawImage.texture = texture; mRawImage.enabled = false; } else { mRawImage.texture = texture; mRawImage.enabled = true; } } public WebRawImage(RawImage mRawImage, string web_url) { this.mRawImage = mRawImage; this.identifyId = web_url; } } public class WebSprite: WebImage { public SpriteRenderer mRawImage; public override void SetImage(Texture2D texture) { if (texture == null) { mRawImage.sprite = null; mRawImage.gameObject.SetActive(false); } else { mRawImage.sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), new Vector2(0.5f,0.5f)); if(texture.width>texture.height) { mRawImage.transform.localScale = Vector3.one * 0.1f * (texture.width / 1024f); } else { mRawImage.transform.localScale = Vector3.one * 0.1f * (texture.height / 1024f); } mRawImage.gameObject.SetActive(true); } } public WebSprite(SpriteRenderer mRawImage, string web_url) { this.mRawImage = mRawImage; this.identifyId = web_url; } } public class WebImageTool: MonoBehaviour { private string format_name = "{0}/{1}"; private static WebImageTool _instance = null; public static WebImageTool Instance { get { if (_instance == null) { GameObject go = new GameObject(); go.name = "WebImageTool"; DontDestroyOnLoad(go); _instance = go.AddComponent(); } return _instance; } } public void LoadImage(WebRawImage img) { if(img.identifyId == null || img.identifyId.Equals("")) { img.SetImage(null); return; } StartCoroutine(LoadTexture(img)); } public void LoadImage(WebSprite img) { if (img.identifyId == null || img.identifyId.Equals("")) { img.SetImage(null); return; } StartCoroutine(LoadTexture(img)); } private string LocalURL(string url) { var strusers2 = url.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string str1 = string.Join("/", strusers2);//array为数组 return string.Format(format_name, GameEnum.asset_path, str1); } /// /// 判断是否本地有缓存,如果有则读取本地的资源,否则读取网络资源 /// /// /// private IEnumerator LoadTexture(WebImage img) { if (!string.IsNullOrEmpty(GamePlayerData.Instance.url_image_str)) { if (!File.Exists(LocalURL(img.identifyId))) { yield return LoadNetWorkTexture(img); } else { yield return LoadLocalTexture(img); } } } /// /// 本地已缓存 /// /// /// private IEnumerator LoadLocalTexture(WebImage img) { //已在本地缓存 string filePath = "file:///" + LocalURL(img.identifyId); CDebug.Log("本地文件路径 " + filePath); WWW www = new WWW(filePath); yield return www; img.SetImage(www.texture); } /// /// 本地未缓存 /// /// /// private IEnumerator LoadNetWorkTexture(WebImage img) { string full_url = string.Format(format_name, GamePlayerData.Instance.url_image_str, img.identifyId); CDebug.Log("full_url : " + full_url); string img_url = (new Uri(full_url)).AbsoluteUri; CDebug.Log(img_url); WWW www = new WWW(img_url); yield return www; CDebug.Log("尝试网络加载"); #if UNITY_EDITOR || UNITY_STANDALONE_WIN if (!Directory.Exists(GameEnum.asset_path)) { Directory.CreateDirectory(GameEnum.asset_path);//创建新路径 } #endif try { //判断网络状态 if (Application.internetReachability != NetworkReachability.NotReachable) { //Debug.Log(Application.internetReachability); img.SetImage(www.texture); //将图片保存至缓存路径 byte[] pngData = www.texture.EncodeToPNG(); string png_path = LocalURL(img.identifyId); string[] path_ary = png_path.Split('/'); path_ary[path_ary.Length - 1] = ""; string file_path = string.Join("/", path_ary);//array为数组 if (!Directory.Exists(file_path)) { Directory.CreateDirectory(file_path);//创建新路径 } File.WriteAllBytes(png_path, pngData); } } catch { Debug.Log("网络资源本地失败"); } } }