WebImageTool.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using System;
  6. using System.IO;
  7. using UnityEngine.UI;
  8. public class WebImage
  9. {
  10. public string identifyId;
  11. public virtual void SetImage(Texture2D texture)
  12. {
  13. }
  14. }
  15. public class WebRawImage: WebImage
  16. {
  17. public RawImage mRawImage;
  18. public override void SetImage(Texture2D texture)
  19. {
  20. if (texture == null)
  21. {
  22. mRawImage.texture = texture;
  23. mRawImage.enabled = false;
  24. }
  25. else
  26. {
  27. mRawImage.texture = texture;
  28. mRawImage.enabled = true;
  29. }
  30. }
  31. public WebRawImage(RawImage mRawImage, string web_url)
  32. {
  33. this.mRawImage = mRawImage;
  34. this.identifyId = web_url;
  35. }
  36. }
  37. public class WebSprite: WebImage
  38. {
  39. public SpriteRenderer mRawImage;
  40. public override void SetImage(Texture2D texture)
  41. {
  42. if (texture == null)
  43. {
  44. mRawImage.sprite = null;
  45. mRawImage.gameObject.SetActive(false);
  46. }
  47. else
  48. {
  49. mRawImage.sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), new Vector2(0.5f,0.5f));
  50. if(texture.width>texture.height)
  51. {
  52. mRawImage.transform.localScale = Vector3.one * 0.1f * (texture.width / 1024f);
  53. }
  54. else
  55. {
  56. mRawImage.transform.localScale = Vector3.one * 0.1f * (texture.height / 1024f);
  57. }
  58. mRawImage.gameObject.SetActive(true);
  59. }
  60. }
  61. public WebSprite(SpriteRenderer mRawImage, string web_url)
  62. {
  63. this.mRawImage = mRawImage;
  64. this.identifyId = web_url;
  65. }
  66. }
  67. public class WebImageTool: MonoBehaviour
  68. {
  69. private string format_name = "{0}/{1}";
  70. private static WebImageTool _instance = null;
  71. public static WebImageTool Instance
  72. {
  73. get
  74. {
  75. if (_instance == null)
  76. {
  77. GameObject go = new GameObject();
  78. go.name = "WebImageTool";
  79. DontDestroyOnLoad(go);
  80. _instance = go.AddComponent<WebImageTool>();
  81. }
  82. return _instance;
  83. }
  84. }
  85. public void LoadImage(WebRawImage img)
  86. {
  87. if(img.identifyId == null || img.identifyId.Equals(""))
  88. {
  89. img.SetImage(null);
  90. return;
  91. }
  92. StartCoroutine(LoadTexture(img));
  93. }
  94. public void LoadImage(WebSprite img)
  95. {
  96. if (img.identifyId == null || img.identifyId.Equals(""))
  97. {
  98. img.SetImage(null);
  99. return;
  100. }
  101. StartCoroutine(LoadTexture(img));
  102. }
  103. private string LocalURL(string url)
  104. {
  105. var strusers2 = url.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  106. string str1 = string.Join("/", strusers2);//array为数组
  107. return string.Format(format_name, GameEnum.asset_path, str1);
  108. }
  109. /// <summary>
  110. /// 判断是否本地有缓存,如果有则读取本地的资源,否则读取网络资源
  111. /// </summary>
  112. /// <param name="url"></param>
  113. /// <returns></returns>
  114. private IEnumerator LoadTexture(WebImage img)
  115. {
  116. if (!string.IsNullOrEmpty(GamePlayerData.Instance.url_image_str))
  117. {
  118. if (!File.Exists(LocalURL(img.identifyId)))
  119. {
  120. yield return LoadNetWorkTexture(img);
  121. }
  122. else
  123. {
  124. yield return LoadLocalTexture(img);
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// 本地已缓存
  130. /// </summary>
  131. /// <param name="url"></param>
  132. /// <returns></returns>
  133. private IEnumerator LoadLocalTexture(WebImage img)
  134. {
  135. //已在本地缓存
  136. string filePath = "file:///" + LocalURL(img.identifyId);
  137. CDebug.Log("本地文件路径 " + filePath);
  138. WWW www = new WWW(filePath);
  139. yield return www;
  140. img.SetImage(www.texture);
  141. }
  142. /// <summary>
  143. /// 本地未缓存
  144. /// </summary>
  145. /// <param name="url"></param>
  146. /// <returns></returns>
  147. private IEnumerator LoadNetWorkTexture(WebImage img)
  148. {
  149. string full_url = string.Format(format_name, GamePlayerData.Instance.url_image_str, img.identifyId);
  150. CDebug.Log("full_url : " + full_url);
  151. string img_url = (new Uri(full_url)).AbsoluteUri;
  152. CDebug.Log(img_url);
  153. WWW www = new WWW(img_url);
  154. yield return www;
  155. CDebug.Log("尝试网络加载");
  156. #if UNITY_EDITOR || UNITY_STANDALONE_WIN
  157. if (!Directory.Exists(GameEnum.asset_path))
  158. {
  159. Directory.CreateDirectory(GameEnum.asset_path);//创建新路径
  160. }
  161. #endif
  162. try
  163. {
  164. //判断网络状态
  165. if (Application.internetReachability != NetworkReachability.NotReachable)
  166. {
  167. //Debug.Log(Application.internetReachability);
  168. img.SetImage(www.texture);
  169. //将图片保存至缓存路径
  170. byte[] pngData = www.texture.EncodeToPNG();
  171. string png_path = LocalURL(img.identifyId);
  172. string[] path_ary = png_path.Split('/');
  173. path_ary[path_ary.Length - 1] = "";
  174. string file_path = string.Join("/", path_ary);//array为数组
  175. if (!Directory.Exists(file_path))
  176. {
  177. Directory.CreateDirectory(file_path);//创建新路径
  178. }
  179. File.WriteAllBytes(png_path, pngData);
  180. }
  181. }
  182. catch
  183. {
  184. Debug.Log("网络资源本地失败");
  185. }
  186. }
  187. }