SetWebGLText.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 Downloadip()
  21. {
  22. Debug.Log("开始获取ip文件" + Application.streamingAssetsPath + "/1.txt");
  23. UnityWebRequest m_webrequest = UnityWebRequest.Get(Application.streamingAssetsPath + "/1.txt");
  24. yield return m_webrequest.SendWebRequest();
  25. if (m_webrequest.result != UnityWebRequest.Result.Success)
  26. {
  27. Debug.Log("获取ip文件失败==》" + m_webrequest.downloadHandler.error);
  28. Debug.LogError("Failed to download image");
  29. }
  30. else
  31. {
  32. DataManager.ip = RemoveWhitespace(m_webrequest.downloadHandler.text);
  33. Debug.Log("获取ip文件成功==》" + DataManager.ip);
  34. }
  35. }
  36. string RemoveWhitespace(string input)
  37. {
  38. // 去除回车、换行、制表符和空格等空白字符
  39. return input.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
  40. }
  41. IEnumerator DownloadMacIp()
  42. {
  43. string filePath = Application.streamingAssetsPath+"/1.txt";
  44. Debug.Log("开始获取ip文件"+ filePath);
  45. if (!stlist.ContainsKey(filePath))
  46. {
  47. if (System.IO.File.Exists(filePath))
  48. {
  49. Task<string> fileData = System.IO.File.ReadAllTextAsync(filePath);
  50. while (!fileData.IsCompleted)
  51. {
  52. yield return null;
  53. }
  54. DataManager.ip = RemoveWhitespace(fileData.Result);
  55. }
  56. }
  57. else
  58. {
  59. yield return null;
  60. }
  61. }
  62. IEnumerator DownloadImageMac(callback url)
  63. {
  64. string filePath = url.url;
  65. if (!stlist.ContainsKey(url.url))
  66. {
  67. if (System.IO.File.Exists(filePath))
  68. {
  69. Task<byte[]> fileData = System.IO.File.ReadAllBytesAsync(filePath);
  70. while (!fileData.IsCompleted)
  71. {
  72. yield return null;
  73. }
  74. Texture2D texture = new Texture2D(2, 2);
  75. if (texture.LoadImage(fileData.Result))
  76. {
  77. stlist.Add(url.url, texture);
  78. url.msg.Invoke(texture);
  79. isload = false;
  80. }
  81. }
  82. else
  83. {
  84. isload = false;
  85. Debug.LogError("Failed to download image "+ filePath);
  86. }
  87. }
  88. else
  89. {
  90. yield return null;
  91. url.msg.Invoke(stlist[url.url]);
  92. isload = false;
  93. }
  94. }
  95. IEnumerator LoadAB()
  96. {
  97. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "stzhongs");
  98. //UnityWebRequest request = UnityWebRequest.Get(filePath);
  99. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
  100. {
  101. yield return req.SendWebRequest();
  102. if (req.error == null)
  103. {
  104. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  105. webglfont = ab.LoadAsset<Font>("stzhongs");
  106. }
  107. else
  108. {
  109. Debug.Log(filePath );
  110. }
  111. }
  112. }
  113. Queue<callback> dl = new Queue<callback>();
  114. public void GetTexture(string url,Action<Texture2D> msg)
  115. {
  116. if(stlist.ContainsKey(url))
  117. {
  118. msg.Invoke(stlist[url]);
  119. }
  120. else
  121. {
  122. callback cb = new callback();
  123. cb.url = url;
  124. cb.msg = msg;
  125. dl.Enqueue(cb);
  126. }
  127. }
  128. bool isload;
  129. private void Update()
  130. {
  131. if(dl.Count>0&&!isload)
  132. {
  133. isload = true;
  134. callback cb = dl.Dequeue();
  135. #if UNITY_EDITOR
  136. StartCoroutine(DownloadImageMac(cb));
  137. #else
  138. StartCoroutine(DownloadImage(cb));
  139. #endif
  140. #if UNITY_EDITOR
  141. StartCoroutine(DownloadMacIp());
  142. #else
  143. StartCoroutine(Downloadip());
  144. #endif
  145. }
  146. }
  147. Dictionary<string, Texture2D> stlist = new Dictionary<string, Texture2D>();
  148. IEnumerator DownloadImage(callback url)
  149. {
  150. if(!stlist.ContainsKey(url.url))
  151. {
  152. UnityWebRequest m_webrequest = UnityWebRequestTexture.GetTexture(url.url);
  153. yield return m_webrequest.SendWebRequest();
  154. if (m_webrequest.result != UnityWebRequest.Result.Success)
  155. {
  156. isload = false;
  157. Debug.LogError("Failed to download image");
  158. }
  159. else
  160. {
  161. Texture2D tex = ((DownloadHandlerTexture)m_webrequest.downloadHandler).texture;
  162. stlist.Add(url.url, tex);
  163. url.msg.Invoke(tex);
  164. isload = false;
  165. }
  166. }
  167. else
  168. {
  169. yield return null;
  170. url.msg.Invoke(stlist[url.url]);
  171. isload = false;
  172. }
  173. }
  174. public class callback
  175. {
  176. public string url;
  177. public Action<Texture2D> msg;
  178. }
  179. }