SetWebGLText.cs 6.0 KB

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