SetWebGLText.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. [ExecuteInEditMode]
  8. public class SetWebGLText : MonoBehaviour
  9. {
  10. public static SetWebGLText Instance;
  11. public static Font webglfont;
  12. // public Font font;
  13. // Start is called before the first frame update
  14. void Awake()
  15. {
  16. Instance = this;
  17. // webglfont = font;
  18. StartCoroutine(LoadAB());
  19. }
  20. IEnumerator DownloadImageMac(callback url)
  21. {
  22. string filePath = url.url;
  23. if (!stlist.ContainsKey(url.url))
  24. {
  25. if (System.IO.File.Exists(filePath))
  26. {
  27. Task<byte[]> fileData = System.IO.File.ReadAllBytesAsync(filePath);
  28. while (!fileData.IsCompleted)
  29. {
  30. yield return null;
  31. }
  32. Texture2D texture = new Texture2D(2, 2);
  33. if (texture.LoadImage(fileData.Result))
  34. {
  35. if(stlist.Count>10)
  36. {
  37. foreach(var key in stlist.Keys)
  38. {
  39. stlist.Remove(key);
  40. break;
  41. }
  42. }
  43. if(!url.isyongjiu)
  44. stlist.Add(url.url, texture);
  45. url.msg.Invoke(texture);
  46. isload = false;
  47. }
  48. }
  49. else
  50. {
  51. isload = false;
  52. Debug.LogError("Failed to download image "+ filePath);
  53. }
  54. }
  55. else
  56. {
  57. yield return null;
  58. url.msg.Invoke(stlist[url.url]);
  59. isload = false;
  60. }
  61. }
  62. public static TaskConfig tc;
  63. IEnumerator LoadAB()
  64. {
  65. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "loadab");
  66. //UnityWebRequest request = UnityWebRequest.Get(filePath);
  67. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
  68. {
  69. yield return req.SendWebRequest();
  70. if (req.error == null)
  71. {
  72. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  73. webglfont = ab.LoadAsset<Font>("font");
  74. // UI.SetActive(true);
  75. }
  76. else
  77. {
  78. Debug.Log(filePath );
  79. }
  80. }
  81. }
  82. Queue<callbackBD> dlbd = new Queue<callbackBD>();
  83. public void GetBD(string url, Action<GameObject> msg)
  84. {
  85. if (sbdtlist.ContainsKey(url))
  86. {
  87. msg.Invoke(sbdtlist[url]);
  88. }
  89. else
  90. {
  91. callbackBD cb = new callbackBD();
  92. cb.url = url;
  93. cb.msg = msg;
  94. dlbd.Enqueue(cb);
  95. }
  96. }
  97. Queue<callback> dl = new Queue<callback>();
  98. public void GetTexture(string url,Action<Texture2D> msg,bool isyongjiu=false)
  99. {
  100. if(stlist.ContainsKey(url))
  101. {
  102. msg.Invoke(stlist[url]);
  103. }
  104. else
  105. {
  106. callback cb = new callback();
  107. cb.isyongjiu =isyongjiu;
  108. cb.url = url;
  109. cb.msg = msg;
  110. dl.Enqueue(cb);
  111. }
  112. }
  113. bool isload;
  114. bool isbdload;
  115. private void Update()
  116. {
  117. if (dl.Count > 0 && !isload)
  118. {
  119. isload = true;
  120. callback cb = dl.Dequeue();
  121. #if UNITY_EDITOR
  122. StartCoroutine(DownloadImageMac(cb));
  123. #else
  124. StartCoroutine(DownloadImage(cb));
  125. #endif
  126. }
  127. if (dlbd.Count > 0 && !isbdload)
  128. {
  129. isbdload = true;
  130. callbackBD cb = dlbd.Dequeue();
  131. StartCoroutine(DownloadBD(cb));
  132. }
  133. }
  134. IEnumerator DownloadBD(callbackBD cbd)
  135. {
  136. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath+"/bd", cbd.url);
  137. //UnityWebRequest request = UnityWebRequest.Get(filePath);
  138. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
  139. {
  140. yield return req.SendWebRequest();
  141. if (req.error == null)
  142. {
  143. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  144. GameObject go = GameObject.Instantiate( ab.LoadAsset<GameObject>(cbd.url));
  145. cbd.msg.Invoke(go);
  146. }
  147. else
  148. {
  149. Debug.Log(filePath);
  150. }
  151. }
  152. }
  153. public void clearTexture()
  154. {
  155. foreach (string i in stlist.Keys)
  156. {
  157. Destroy(stlist[i]);
  158. }
  159. stlist.Clear();
  160. }
  161. public Dictionary<string, Texture2D> stlist = new Dictionary<string, Texture2D>();
  162. Dictionary<string, GameObject> sbdtlist = new Dictionary<string, GameObject>();
  163. IEnumerator DownloadImage(callback url)
  164. {
  165. if(!stlist.ContainsKey(url.url))
  166. {
  167. UnityWebRequest m_webrequest = UnityWebRequestTexture.GetTexture(url.url);
  168. yield return m_webrequest.SendWebRequest();
  169. if (m_webrequest.result != UnityWebRequest.Result.Success)
  170. {
  171. isload = false;
  172. Debug.LogError("Failed to download image");
  173. }
  174. else
  175. {
  176. Texture2D tex = ((DownloadHandlerTexture)m_webrequest.downloadHandler).texture;
  177. if(!url.isyongjiu)
  178. stlist.Add(url.url, tex);
  179. url.msg.Invoke(tex);
  180. isload = false;
  181. }
  182. }
  183. else
  184. {
  185. yield return null;
  186. url.msg.Invoke(stlist[url.url]);
  187. isload = false;
  188. }
  189. }
  190. public class callback
  191. {
  192. public bool isyongjiu;
  193. public string url;
  194. public Action<Texture2D> msg;
  195. }
  196. public class callbackBD
  197. {
  198. public string url;
  199. public Action<GameObject> msg;
  200. }
  201. }