123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using LitJson;
- using UnityEngine;
- using UnityEngine.Networking;
- [ExecuteInEditMode]
- public class SetWebGLText : MonoBehaviour
- {
- public static SetWebGLText Instance;
- public static Font webglfont;
- public Font font;
-
- void Awake()
- {
- Instance = this;
- webglfont = font;
-
- #if UNITY_EDITOR
- StartCoroutine(DownloadMacQXZH());
- #else
- StartCoroutine(DownloadQX());
- #endif
- }
- IEnumerator DownloadQX()
- {
- yield return null;
- Debug.Log("开始获取ip文件" + Application.streamingAssetsPath + "/account.txt");
- UnityWebRequest m_webrequest = UnityWebRequest.Get(Application.streamingAssetsPath + "/account.txt");
- yield return m_webrequest.SendWebRequest();
- if (m_webrequest.result != UnityWebRequest.Result.Success)
- {
- Debug.Log("获取ip文件失败==》" + m_webrequest.downloadHandler.error);
- Debug.LogError("Failed to download image");
- }
- else
- {
- JsonData d = JsonMapper.ToObject(m_webrequest.downloadHandler.text);
- GameManager.Instance.zh =new Dictionary<string, string>();
- for (int i = 0;i<d["user"].Count;i++)
- {
- GameManager.Instance.zh.Add(d["user"][i]["acc"].ToString(),d["user"][i]["pass"].ToString());
- }
- for (int i = 0;i<d["per"].Count;i++)
- {
- List<int> inlist=new List<int>();
- for (int j=0;j< d["per"][i]["per"].Count;j++)
- {
- inlist.Add( int.Parse(d["per"][i]["per"][j].ToString()));
- }
- Debug.Log(d["per"][i]["acc"].ToString());
- if(!GameManager.Instance.qx.ContainsKey(d["per"][i]["acc"].ToString()))
- GameManager.Instance.qx.Add(d["per"][i]["acc"].ToString(),inlist);
- }
- }
- }
- IEnumerator Downloadip()
- {
- yield return null;
- if(Application.streamingAssetsPath.Contains("/nomodel/StreamingAssets"))
- {
- DeMaDataManager.ip = Application.streamingAssetsPath.Split("/nomodel/StreamingAssets")[0]+"/api";
- }
- else
- {
- DeMaDataManager.ip = Application.streamingAssetsPath.Split("/StreamingAssets")[0]+"/api";
- }
- }
- string RemoveWhitespace(string input)
- {
-
- return input.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
- }
- IEnumerator DownloadMacIp()
- {
- string filePath = Application.streamingAssetsPath+"/1.txt";
- Debug.Log("开始获取ip文件"+ filePath);
- if (!stlist.ContainsKey(filePath))
- {
- if (System.IO.File.Exists(filePath))
- {
- Task<string> fileData = System.IO.File.ReadAllTextAsync(filePath);
- while (!fileData.IsCompleted)
- {
- yield return null;
- }
- DataManager.ip = RemoveWhitespace(fileData.Result);
- DeMaDataManager.ip = RemoveWhitespace(fileData.Result);
- }
- else{
-
- StartCoroutine(DownloadMacIp());
- }
- }
- else
- {
- yield return null;
- }
- }
- IEnumerator DownloadMacQXZH()
- {
- string filePath = Application.streamingAssetsPath+"/account.txt";
- Debug.Log("开始获取DownloadMacQXZH文件"+ filePath);
- if (!stlist.ContainsKey(filePath))
- {
- if (System.IO.File.Exists(filePath))
- {
- Task<string> fileData = System.IO.File.ReadAllTextAsync(filePath);
- while (!fileData.IsCompleted)
- {
- yield return null;
- }
- JsonData d = JsonMapper.ToObject(fileData.Result);
- GameManager.Instance.zh =new Dictionary<string, string>();
- for (int i = 0;i<d["user"].Count;i++)
- {
- GameManager.Instance.zh.Add(d["user"][i]["acc"].ToString(),d["user"][i]["pass"].ToString());
- }
- for (int i = 0;i<d["per"].Count;i++)
- {
- List<int> inlist=new List<int>();
- for (int j=0;j< d["per"][i]["per"].Count;j++)
- {
- inlist.Add( int.Parse(d["per"][i]["per"][j].ToString()));
- }
- Debug.Log(d["per"][i]["acc"].ToString());
- if(!GameManager.Instance.qx.ContainsKey(d["per"][i]["acc"].ToString()))
- GameManager.Instance.qx.Add(d["per"][i]["acc"].ToString(),inlist);
- }
- }
- else{
-
- StartCoroutine(DownloadMacIp());
- }
- }
- else
- {
- yield return null;
- }
- }
- IEnumerator DownloadImageMac(callback url)
- {
- string filePath = url.url;
- if (!stlist.ContainsKey(url.url))
- {
- if (System.IO.File.Exists(filePath))
- {
- Task<byte[]> fileData = System.IO.File.ReadAllBytesAsync(filePath);
- while (!fileData.IsCompleted)
- {
- yield return null;
- }
- Texture2D texture = new Texture2D(2, 2);
- if (texture.LoadImage(fileData.Result))
- {
- stlist.Add(url.url, texture);
- url.msg.Invoke(texture);
- isload = false;
- }
- }
- else
- {
- isload = false;
- Debug.LogError("Failed to download image "+ filePath);
- }
- }
- else
- {
- yield return null;
- url.msg.Invoke(stlist[url.url]);
- isload = false;
- }
- }
- IEnumerator LoadAB()
- {
- string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "stzhongs");
-
- using (UnityWebRequest req = UnityWebRequestAssetBundle.GetAssetBundle(filePath))
- {
- yield return req.SendWebRequest();
- if (req.error == null)
- {
- AssetBundle ab = (req.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
- webglfont = ab.LoadAsset<Font>("stzhongs");
- }
- else
- {
- Debug.Log(filePath );
- }
- }
- }
- Queue<callback> dl = new Queue<callback>();
- public void GetTexture(string url,Action<Texture2D> msg)
- {
- if(stlist.ContainsKey(url))
- {
- msg.Invoke(stlist[url]);
- }
- else
- {
- callback cb = new callback();
- cb.url = url;
- cb.msg = msg;
- dl.Enqueue(cb);
- }
- }
- bool isload;
- private void Update()
- {
- if(dl.Count>0&&!isload)
- {
- isload = true;
- callback cb = dl.Dequeue();
- #if UNITY_EDITOR
- StartCoroutine(DownloadImageMac(cb));
- #else
- StartCoroutine(DownloadImage(cb));
- #endif
- #if UNITY_EDITOR
- StartCoroutine(DownloadMacIp());
- #else
- StartCoroutine(Downloadip());
- #endif
- }
- }
- Dictionary<string, Texture2D> stlist = new Dictionary<string, Texture2D>();
- IEnumerator DownloadImage(callback url)
- {
- if(!stlist.ContainsKey(url.url))
- {
- UnityWebRequest m_webrequest = UnityWebRequestTexture.GetTexture(url.url);
- yield return m_webrequest.SendWebRequest();
- if (m_webrequest.result != UnityWebRequest.Result.Success)
- {
- isload = false;
- Debug.LogError("Failed to download image");
- }
- else
- {
- Texture2D tex = ((DownloadHandlerTexture)m_webrequest.downloadHandler).texture;
- stlist.Add(url.url, tex);
- url.msg.Invoke(tex);
- isload = false;
- }
- }
- else
- {
- yield return null;
- url.msg.Invoke(stlist[url.url]);
- isload = false;
- }
- }
- public class callback
- {
- public string url;
- public Action<Texture2D> msg;
- }
-
- }
|