SetWebGLText.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 GameObject UI;
  11. public static SetWebGLText Instance;
  12. public static Font webglfont;
  13. public Font font;
  14. // Start is called before the first frame update
  15. void Awake()
  16. {
  17. Instance = this;
  18. webglfont = font;
  19. // StartCoroutine(LoadAB());
  20. }
  21. IEnumerator Downloadip()
  22. {
  23. Debug.Log("开始获取ip文件" + Application.streamingAssetsPath + "/1.txt");
  24. UnityWebRequest m_webrequest = UnityWebRequest.Get(Application.streamingAssetsPath + "/1.txt");
  25. yield return m_webrequest.SendWebRequest();
  26. if (m_webrequest.result != UnityWebRequest.Result.Success)
  27. {
  28. Debug.Log("获取ip文件失败==》" + m_webrequest.downloadHandler.error);
  29. Debug.LogError("Failed to download image");
  30. }
  31. else
  32. {
  33. DataManager.ip = m_webrequest.downloadHandler.text;
  34. Debug.Log("获取ip文件成功==》" + DataManager.ip);
  35. }
  36. }
  37. IEnumerator DownloadMacIp()
  38. {
  39. string filePath = Application.streamingAssetsPath+"/1.txt";
  40. Debug.Log("开始获取ip文件"+ filePath);
  41. if (!stlist.ContainsKey(filePath))
  42. {
  43. if (System.IO.File.Exists(filePath))
  44. {
  45. Task<string> fileData = System.IO.File.ReadAllTextAsync(filePath);
  46. while (!fileData.IsCompleted)
  47. {
  48. yield return null;
  49. }
  50. DataManager.ip = fileData.Result;
  51. }
  52. }
  53. else
  54. {
  55. yield return null;
  56. }
  57. }
  58. IEnumerator DownloadImageMac(callback url)
  59. {
  60. string filePath = url.url;
  61. if (!stlist.ContainsKey(url.url))
  62. {
  63. if (System.IO.File.Exists(filePath))
  64. {
  65. Task<byte[]> fileData = System.IO.File.ReadAllBytesAsync(filePath);
  66. while (!fileData.IsCompleted)
  67. {
  68. yield return null;
  69. }
  70. Texture2D texture = new Texture2D(2, 2);
  71. if (texture.LoadImage(fileData.Result))
  72. {
  73. stlist.Add(url.url, texture);
  74. url.msg.Invoke(texture);
  75. isload = false;
  76. }
  77. }
  78. else
  79. {
  80. isload = false;
  81. Debug.LogError("Failed to download image "+ filePath);
  82. }
  83. }
  84. else
  85. {
  86. yield return null;
  87. url.msg.Invoke(stlist[url.url]);
  88. isload = false;
  89. }
  90. }
  91. IEnumerator LoadAB()
  92. {
  93. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "stzhongs");
  94. //UnityWebRequest request = UnityWebRequest.Get(filePath);
  95. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
  96. {
  97. yield return req.SendWebRequest();
  98. if (req.error == null)
  99. {
  100. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  101. webglfont = ab.LoadAsset<Font>("stzhongs");
  102. UI.SetActive(true);
  103. }
  104. else
  105. {
  106. Debug.Log(filePath );
  107. }
  108. }
  109. }
  110. Queue<callback> dl = new Queue<callback>();
  111. public void GetTexture(string url,Action<Texture2D> msg)
  112. {
  113. if(stlist.ContainsKey(url))
  114. {
  115. msg.Invoke(stlist[url]);
  116. }
  117. else
  118. {
  119. callback cb = new callback();
  120. cb.url = url;
  121. cb.msg = msg;
  122. dl.Enqueue(cb);
  123. }
  124. }
  125. bool isload;
  126. private void Update()
  127. {
  128. if(dl.Count>0&&!isload)
  129. {
  130. isload = true;
  131. callback cb = dl.Dequeue();
  132. #if UNITY_EDITOR
  133. StartCoroutine(DownloadImageMac(cb));
  134. #else
  135. StartCoroutine(DownloadImage(cb));
  136. #endif
  137. #if UNITY_EDITOR
  138. StartCoroutine(DownloadMacIp());
  139. #else
  140. StartCoroutine(Downloadip());
  141. #endif
  142. }
  143. }
  144. Dictionary<string, Texture2D> stlist = new Dictionary<string, Texture2D>();
  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. }