SetWebGLText.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using LitJson;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. [ExecuteInEditMode]
  9. public class SetWebGLText : MonoBehaviour
  10. {
  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. #if UNITY_EDITOR
  21. StartCoroutine(DownloadMacQXZH());
  22. #else
  23. StartCoroutine(DownloadQX());
  24. #endif
  25. }
  26. IEnumerator DownloadQX()
  27. {
  28. yield return null;
  29. Debug.Log("开始获取ip文件" + Application.streamingAssetsPath + "/account.txt");
  30. UnityWebRequest m_webrequest = UnityWebRequest.Get(Application.streamingAssetsPath + "/account.txt");
  31. yield return m_webrequest.SendWebRequest();
  32. if (m_webrequest.result != UnityWebRequest.Result.Success)
  33. {
  34. Debug.Log("获取ip文件失败==》" + m_webrequest.downloadHandler.error);
  35. Debug.LogError("Failed to download image");
  36. }
  37. else
  38. {
  39. JsonData d = JsonMapper.ToObject(m_webrequest.downloadHandler.text);
  40. GameManager.Instance.zh =new Dictionary<string, string>();
  41. for (int i = 0;i<d["user"].Count;i++)
  42. {
  43. GameManager.Instance.zh.Add(d["user"][i]["acc"].ToString(),d["user"][i]["pass"].ToString());
  44. }
  45. for (int i = 0;i<d["per"].Count;i++)
  46. {
  47. List<int> inlist=new List<int>();
  48. for (int j=0;j< d["per"][i]["per"].Count;j++)
  49. {
  50. inlist.Add( int.Parse(d["per"][i]["per"][j].ToString()));
  51. }
  52. Debug.Log(d["per"][i]["acc"].ToString());
  53. if(!GameManager.Instance.qx.ContainsKey(d["per"][i]["acc"].ToString()))
  54. GameManager.Instance.qx.Add(d["per"][i]["acc"].ToString(),inlist);
  55. }
  56. }
  57. }
  58. IEnumerator Downloadip()
  59. {
  60. yield return null;
  61. if(Application.streamingAssetsPath.Contains("/nomodel/StreamingAssets"))
  62. {
  63. DeMaDataManager.ip = Application.streamingAssetsPath.Split("/nomodel/StreamingAssets")[0]+"/api";
  64. }
  65. else
  66. {
  67. DeMaDataManager.ip = Application.streamingAssetsPath.Split("/StreamingAssets")[0]+"/api";
  68. }
  69. /*
  70. Debug.Log("开始获取ip文件" + Application.streamingAssetsPath + "/1.txt");
  71. UnityWebRequest m_webrequest = UnityWebRequest.Get(Application.streamingAssetsPath + "/1.txt");
  72. yield return m_webrequest.SendWebRequest();
  73. if (m_webrequest.result != UnityWebRequest.Result.Success)
  74. {
  75. Debug.Log("获取ip文件失败==》" + m_webrequest.downloadHandler.error);
  76. Debug.LogError("Failed to download image");
  77. }
  78. else
  79. {
  80. DataManager.ip = RemoveWhitespace(m_webrequest.downloadHandler.text);
  81. DeMaDataManager.ip = RemoveWhitespace(m_webrequest.downloadHandler.text);
  82. Debug.Log("获取ip文件成功==》" + DataManager.ip);
  83. }*/
  84. }
  85. string RemoveWhitespace(string input)
  86. {
  87. // 去除回车、换行、制表符和空格等空白字符
  88. return input.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
  89. }
  90. IEnumerator DownloadMacIp()
  91. {
  92. string filePath = Application.streamingAssetsPath+"/1.txt";
  93. Debug.Log("开始获取ip文件"+ filePath);
  94. if (!stlist.ContainsKey(filePath))
  95. {
  96. if (System.IO.File.Exists(filePath))
  97. {
  98. Task<string> fileData = System.IO.File.ReadAllTextAsync(filePath);
  99. while (!fileData.IsCompleted)
  100. {
  101. yield return null;
  102. }
  103. DataManager.ip = RemoveWhitespace(fileData.Result);
  104. DeMaDataManager.ip = RemoveWhitespace(fileData.Result);
  105. }
  106. else{
  107. StartCoroutine(DownloadMacIp());
  108. }
  109. }
  110. else
  111. {
  112. yield return null;
  113. }
  114. }
  115. IEnumerator DownloadMacQXZH()
  116. {
  117. string filePath = Application.streamingAssetsPath+"/account.txt";
  118. Debug.Log("开始获取DownloadMacQXZH文件"+ filePath);
  119. if (!stlist.ContainsKey(filePath))
  120. {
  121. if (System.IO.File.Exists(filePath))
  122. {
  123. Task<string> fileData = System.IO.File.ReadAllTextAsync(filePath);
  124. while (!fileData.IsCompleted)
  125. {
  126. yield return null;
  127. }
  128. JsonData d = JsonMapper.ToObject(fileData.Result);
  129. GameManager.Instance.zh =new Dictionary<string, string>();
  130. for (int i = 0;i<d["user"].Count;i++)
  131. {
  132. GameManager.Instance.zh.Add(d["user"][i]["acc"].ToString(),d["user"][i]["pass"].ToString());
  133. }
  134. for (int i = 0;i<d["per"].Count;i++)
  135. {
  136. List<int> inlist=new List<int>();
  137. for (int j=0;j< d["per"][i]["per"].Count;j++)
  138. {
  139. inlist.Add( int.Parse(d["per"][i]["per"][j].ToString()));
  140. }
  141. Debug.Log(d["per"][i]["acc"].ToString());
  142. if(!GameManager.Instance.qx.ContainsKey(d["per"][i]["acc"].ToString()))
  143. GameManager.Instance.qx.Add(d["per"][i]["acc"].ToString(),inlist);
  144. }
  145. }
  146. else{
  147. StartCoroutine(DownloadMacIp());
  148. }
  149. }
  150. else
  151. {
  152. yield return null;
  153. }
  154. }
  155. IEnumerator DownloadImageMac(callback url)
  156. {
  157. string filePath = url.url;
  158. if (!stlist.ContainsKey(url.url))
  159. {
  160. if (System.IO.File.Exists(filePath))
  161. {
  162. Task<byte[]> fileData = System.IO.File.ReadAllBytesAsync(filePath);
  163. while (!fileData.IsCompleted)
  164. {
  165. yield return null;
  166. }
  167. Texture2D texture = new Texture2D(2, 2);
  168. if (texture.LoadImage(fileData.Result))
  169. {
  170. stlist.Add(url.url, texture);
  171. url.msg.Invoke(texture);
  172. isload = false;
  173. }
  174. }
  175. else
  176. {
  177. isload = false;
  178. Debug.LogError("Failed to download image "+ filePath);
  179. }
  180. }
  181. else
  182. {
  183. yield return null;
  184. url.msg.Invoke(stlist[url.url]);
  185. isload = false;
  186. }
  187. }
  188. IEnumerator LoadAB()
  189. {
  190. string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "stzhongs");
  191. //UnityWebRequest request = UnityWebRequest.Get(filePath);
  192. using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
  193. {
  194. yield return req.SendWebRequest();
  195. if (req.error == null)
  196. {
  197. AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  198. webglfont = ab.LoadAsset<Font>("stzhongs");
  199. }
  200. else
  201. {
  202. Debug.Log(filePath );
  203. }
  204. }
  205. }
  206. Queue<callback> dl = new Queue<callback>();
  207. public void GetTexture(string url,Action<Texture2D> msg)
  208. {
  209. if(stlist.ContainsKey(url))
  210. {
  211. msg.Invoke(stlist[url]);
  212. }
  213. else
  214. {
  215. callback cb = new callback();
  216. cb.url = url;
  217. cb.msg = msg;
  218. dl.Enqueue(cb);
  219. }
  220. }
  221. bool isload;
  222. private void Update()
  223. {
  224. if(dl.Count>0&&!isload)
  225. {
  226. isload = true;
  227. callback cb = dl.Dequeue();
  228. #if UNITY_EDITOR
  229. StartCoroutine(DownloadImageMac(cb));
  230. #else
  231. StartCoroutine(DownloadImage(cb));
  232. #endif
  233. #if UNITY_EDITOR
  234. StartCoroutine(DownloadMacIp());
  235. #else
  236. StartCoroutine(Downloadip());
  237. #endif
  238. }
  239. }
  240. Dictionary<string, Texture2D> stlist = new Dictionary<string, Texture2D>();
  241. IEnumerator DownloadImage(callback url)
  242. {
  243. if(!stlist.ContainsKey(url.url))
  244. {
  245. UnityWebRequest m_webrequest = UnityWebRequestTexture.GetTexture(url.url);
  246. yield return m_webrequest.SendWebRequest();
  247. if (m_webrequest.result != UnityWebRequest.Result.Success)
  248. {
  249. isload = false;
  250. Debug.LogError("Failed to download image");
  251. }
  252. else
  253. {
  254. Texture2D tex = ((DownloadHandlerTexture)m_webrequest.downloadHandler).texture;
  255. stlist.Add(url.url, tex);
  256. url.msg.Invoke(tex);
  257. isload = false;
  258. }
  259. }
  260. else
  261. {
  262. yield return null;
  263. url.msg.Invoke(stlist[url.url]);
  264. isload = false;
  265. }
  266. }
  267. public class callback
  268. {
  269. public string url;
  270. public Action<Texture2D> msg;
  271. }
  272. }