SetWebGLText.cs 5.1 KB

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