SetWebGLText.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. stlist.Add(url.url, texture);
  36. url.msg.Invoke(texture);
  37. isload = false;
  38. }
  39. }
  40. else
  41. {
  42. isload = false;
  43. Debug.LogError("Failed to download image "+ filePath);
  44. }
  45. }
  46. else
  47. {
  48. yield return null;
  49. url.msg.Invoke(stlist[url.url]);
  50. isload = false;
  51. }
  52. }
  53. public static TaskConfig tc;
  54. IEnumerator LoadAB()
  55. {
  56. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "loadab");
  57. //UnityWebRequest request = UnityWebRequest.Get(filePath);
  58. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
  59. {
  60. yield return req.SendWebRequest();
  61. if (req.error == null)
  62. {
  63. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  64. tc = ab.LoadAsset<TaskConfig>("task");
  65. // UI.SetActive(true);
  66. }
  67. else
  68. {
  69. Debug.Log(filePath );
  70. }
  71. }
  72. }
  73. Queue<callbackBD> dlbd = new Queue<callbackBD>();
  74. public void GetBD(string url, Action<GameObject> msg)
  75. {
  76. if (sbdtlist.ContainsKey(url))
  77. {
  78. msg.Invoke(sbdtlist[url]);
  79. }
  80. else
  81. {
  82. callbackBD cb = new callbackBD();
  83. cb.url = url;
  84. cb.msg = msg;
  85. dlbd.Enqueue(cb);
  86. }
  87. }
  88. Queue<callback> dl = new Queue<callback>();
  89. public void GetTexture(string url,Action<Texture2D> msg)
  90. {
  91. if(stlist.ContainsKey(url))
  92. {
  93. msg.Invoke(stlist[url]);
  94. }
  95. else
  96. {
  97. callback cb = new callback();
  98. cb.url = url;
  99. cb.msg = msg;
  100. dl.Enqueue(cb);
  101. }
  102. }
  103. bool isload;
  104. bool isbdload;
  105. private void Update()
  106. {
  107. if (dl.Count > 0 && !isload)
  108. {
  109. isload = true;
  110. callback cb = dl.Dequeue();
  111. #if UNITY_EDITOR
  112. StartCoroutine(DownloadImageMac(cb));
  113. #else
  114. StartCoroutine(DownloadImage(cb));
  115. #endif
  116. }
  117. if (dlbd.Count > 0 && !isbdload)
  118. {
  119. isbdload = true;
  120. callbackBD cb = dlbd.Dequeue();
  121. StartCoroutine(DownloadBD(cb));
  122. }
  123. }
  124. IEnumerator DownloadBD(callbackBD cbd)
  125. {
  126. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath+"/bd", cbd.url);
  127. //UnityWebRequest request = UnityWebRequest.Get(filePath);
  128. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
  129. {
  130. yield return req.SendWebRequest();
  131. if (req.error == null)
  132. {
  133. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  134. GameObject go = GameObject.Instantiate( ab.LoadAsset<GameObject>(cbd.url));
  135. cbd.msg.Invoke(go);
  136. }
  137. else
  138. {
  139. Debug.Log(filePath);
  140. }
  141. }
  142. }
  143. Dictionary<string, Texture2D> stlist = new Dictionary<string, Texture2D>();
  144. Dictionary<string, GameObject> sbdtlist = new Dictionary<string, GameObject>();
  145. IEnumerator DownloadImage(callback url)
  146. {
  147. if(!stlist.ContainsKey(url.url))
  148. {
  149. UnityWebRequest m_webrequest = UnityWebRequestTexture.GetTexture(url.url);
  150. yield return m_webrequest.SendWebRequest();
  151. if (m_webrequest.result != UnityWebRequest.Result.Success)
  152. {
  153. isload = false;
  154. Debug.LogError("Failed to download image");
  155. }
  156. else
  157. {
  158. Texture2D tex = ((DownloadHandlerTexture)m_webrequest.downloadHandler).texture;
  159. stlist.Add(url.url, tex);
  160. url.msg.Invoke(tex);
  161. isload = false;
  162. }
  163. }
  164. else
  165. {
  166. yield return null;
  167. url.msg.Invoke(stlist[url.url]);
  168. isload = false;
  169. }
  170. }
  171. public class callback
  172. {
  173. public string url;
  174. public Action<Texture2D> msg;
  175. }
  176. public class callbackBD
  177. {
  178. public string url;
  179. public Action<GameObject> msg;
  180. }
  181. }