using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.Networking; using System.Text; using System.Security.Cryptography; using System.Net; using System.IO; using Newtonsoft.Json.Linq; /// /// Http Request SDK /// public class HttpTool : MonoBehaviour { private static HttpTool _instacne = null; // private string baseUrl = "http://office.ghz-tech.com:9981"; // private string baseUrl = "https://webapi-fat.shadowcreator.com/100022"; // private string baseUrl = "http://192.168.140.123:8080/guideSystem"; // private string baseUrl = "http://office.ghz-tech.com:3419/guideSystem"; // private string baseUrl = "http://office.ghz-tech.com:3425/guideSystem"; private string baseUrl = "https://office.ghz-tech.com:3424/api"; // private string baseUrl = "http://office.ghz-tech.com:9903/guideSystem"; // private string baseUrl = "http://ghz-tech.com:3419/guideSystem"; // private string baseUrl = "http://192.168.43.142:8080"; private string sKey = "zoo_visit_key"; private string token = ""; Dictionary requestHeader = new Dictionary(); // header public static HttpTool Instance { get { if (_instacne == null) { Debug.LogError("Awake error"); // HttpTool._instacne = gameObject.GetComponent(); } return _instacne; } } void Awake() { DontDestroyOnLoad(gameObject); HttpTool._instacne = gameObject.GetComponent(); //http header 的内容 requestHeader.Add("Content-Type", "application/json"); // requestHeader.Add("sKey", sKey); } public void Get(string methodName, Action callback) { StartCoroutine(GetRequest(methodName, callback)); } private IEnumerator GetRequest(string methodName, Action callback) { // string url = Application.streamingAssetsPath + methodName; string url = baseUrl + methodName; Debug.Log(url); using (UnityWebRequest webRequest = UnityWebRequest.Get(url)) { webRequest.SetRequestHeader("authorization", token); //设置header foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 "); Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); if (callback != null) { callback(null); } } else { if (callback != null) { callback(webRequest.downloadHandler.text); } } } } //public void Post( string methodName, string jsonString,Action callback) //{ // StartCoroutine(Post( methodName, jsonString, callback)); //} //private IEnumerator PostRequest(string methodName, string jsonString, Action callback) //{ //} //jsonString 为json字符串,post提交的数据包为json // Loging 成功获取token 失败返回重新登录 // 选择场景 根据选择的场景,获取当前场景的所有素材信息 // 第一次下载 如果有素材之前没有下载过的,在这里需要进行下载/更新 /// /// 文字 /// /// /// /// public void PostTxt(DownLoadMaterial value, string methodName, string jsonString, Action callback) { StartCoroutine(PostRequestTxt(value, methodName, jsonString, callback)); } private IEnumerator PostRequestTxt(DownLoadMaterial value, string methodName, string jsonString, Action callback) { string url = baseUrl + methodName; Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString)); using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) { byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath); foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 "); Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); if (callback != null) { callback(value, null); } } else { if (callback != null) { File.WriteAllBytes(Application.persistentDataPath + "/Text/" + methodName, webRequest.downloadHandler.data); callback(value, webRequest.downloadHandler.text); } } } } #region Old //public void Post(string methodName, string jsonString,string function, Action callback) //{ // StartCoroutine(PostRequest(methodName, jsonString, function, callback)); //} //private IEnumerator PostRequest(string methodName, string jsonString, string function, Action callback) //{ // string url = baseUrl + methodName; // Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString)); // using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) // { // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); // webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); // foreach (var v in requestHeader) // { // webRequest.SetRequestHeader(v.Key, v.Value); // } // yield return webRequest.SendWebRequest(); // if (webRequest.isHttpError || webRequest.isNetworkError) // { // Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); // if (callback != null) // { // callback(function,null); // } // } // else // { // if (callback != null) // { // callback(function,webRequest.downloadHandler.text); // } // } // } //} #endregion /// /// 图片 /// /// /// /// public void PostImage(DownLoadMaterial value, string methodName, string jsonString, Action callback) { StartCoroutine(PostRequestImage(value, methodName, jsonString, callback)); } private IEnumerator PostRequestImage(DownLoadMaterial value, string methodName, string jsonString, Action callback) { string url = baseUrl + methodName; Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString)); using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) { byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); DownloadHandlerTexture texture = new DownloadHandlerTexture(true); webRequest.downloadHandler = texture; // Debug.Log(value.mObj.localSavePath); // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath); // Debug.Log(value.mObj.localSavePath); webRequest.SetRequestHeader("authorization", token); foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); if (callback != null) { callback(value, null); } ErrorLogPanel.Instance.Show(" 下载图片出现错误 " + jsonString); } else { if (callback != null) { try { string path = Application.persistentDataPath + "/Image"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } byte[] data = new byte[webRequest.downloadHandler.data.Length]; data = webRequest.downloadHandler.data; File.WriteAllBytes(value.mObj.localSavePath, data); } catch (Exception e) { ErrorLogPanel.Instance.Show(" 图片缓存出现错误 " + jsonString); } try { Texture2D texture2 = texture.texture; Sprite createSprite = Sprite.Create(texture2, new Rect(0, 0, texture2.width, texture2.height), Vector2.zero); // Debug.Log(webRequest.downloadHandler.data.Length + " "+ texture2.width); callback(value, createSprite); Texture2D texture3 = texture.texture; } catch (Exception e) { ErrorLogPanel.Instance.Show(" 图片生成出现错误 " + jsonString); } ////转为字节数组 // File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data); } } } } /// /// 视频 /// /// /// /// public void PostVideo(DownLoadMaterial value, string methodName, string jsonString, Action callback) { StartCoroutine(PostRequestVideo(value, methodName, jsonString, callback)); } private IEnumerator PostRequestVideo(DownLoadMaterial value, string methodName, string jsonString, Action callback) { string url = baseUrl + methodName; Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString)); using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) { byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(value.mObj.localSavePath, AudioType.ACC); webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath); webRequest.SetRequestHeader("authorization", token); foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); callback(value, null); ErrorLogPanel.Instance.Show(" 下载视频出现错误 " + jsonString); } else { callback(value, value.mObj.localSavePath); // Debug.Log("文件下载成功 :" + value.mObj.DownloadPath +" "+ webRequest.downloadHandler.data.Length); //Debug.Log(webRequest.downloadHandler.data.Length); try { //string path = Application.persistentDataPath + "/Video"; //if (!Directory.Exists(path)) //{ // Directory.CreateDirectory(path); //} //path = Application.persistentDataPath + "/StreamingAssets/Vuforia"; //if (!Directory.Exists(path)) //{ // Directory.CreateDirectory(path); //} //File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data); } catch (Exception) { ErrorLogPanel.Instance.Show(" 视频缓存出现错误 " + jsonString); } } } } /// /// Bundle 文件 /// /// /// /// public void PostBundle(DownLoadMaterial mObj, string methodName, long time, string jsonString, Action CallBack) { StartCoroutine(PostRequestBundle(mObj, methodName, time, jsonString, CallBack)); } private IEnumerator PostRequestBundle(DownLoadMaterial mObj, string methodName, long time, string jsonString, Action CallBack) { string url = baseUrl + methodName; //using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url, (uint)time, 1)) //{ // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); // // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath); // yield return webRequestAB.SendWebRequest(); // if (webRequestAB.isHttpError || webRequestAB.isNetworkError) // { // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length); // } // else // { // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB); // GameObject obj = bundle.LoadAsset(bundle.GetAllAssetNames()[0]); // List listObjs = new List(); // listObjs.Add(obj); // object value = listObjs; // CallBack(mObj, value); // } //} Dictionary headers = new Dictionary(); headers["Content-Type"] = "application/json"; headers["authorization"] = token; // webRequest.SetRequestHeader("authorization", token); //将文本转为byte数组 byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonString); Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString)); //向HTTP服务器提交Post数据 WWW www = new WWW(url, bs, headers); //等待服务器的响应 yield return www; if (www.error != null) { //获取服务器的错误信息 Debug.LogError(www.error); ErrorLogPanel.Instance.Show(" 下载模型出现错误 " + jsonString); yield return null; } else { try { byte[] data = new byte[www.bytes.Length]; data = www.bytes; Debug.Log(data.Length); string path = Application.persistentDataPath + "/Model"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } File.WriteAllBytes(mObj.mObj.localSavePath, data); } catch (Exception e) { Debug.Log(e.Message.ToString()); //ErrorLogPanel.Instance.Show(" 模型缓存出现错误 " + jsonString); } try { Debug.Log(www.bytes.Length); var ab = www.assetBundle; GameObject obj = ab.LoadAsset(ab.GetAllAssetNames()[0]); List listObjs = new List(); listObjs.Add(obj); object value = listObjs; CallBack(mObj, value); } catch (Exception) { ErrorLogPanel.Instance.Show(" 模型生成出现错误 " + jsonString); } } //using (UnityWebRequest webRequestAB = new UnityWebRequest(url, "POST")) //{ // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath); // yield return webRequestAB.SendWebRequest(); // if (webRequestAB.isHttpError || webRequestAB.isNetworkError) // { // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length); // } // else // { // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB); // GameObject obj = bundle.LoadAsset(bundle.GetAllAssetNames()[0]); // List listObjs = new List(); // listObjs.Add(obj); // object value = listObjs; // CallBack(mObj, value); // } //} } public void PostCDNImage(DownLoadMaterial value, string jsonString,Action callback) { StartCoroutine(PostCDNRequestImage(value, jsonString, callback)); } private IEnumerator PostCDNRequestImage(DownLoadMaterial value, string jsonString, Action callback) { string url = value.mObj.cdnUrl; Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString)); using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) { byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); DownloadHandlerTexture texture = new DownloadHandlerTexture(true); webRequest.downloadHandler = texture; // Debug.Log(value.mObj.localSavePath); // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath); // Debug.Log(value.mObj.localSavePath); webRequest.SetRequestHeader("authorization", token); foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); if (callback != null) { callback(value, null); } ErrorLogPanel.Instance.Show(" 下载图片出现错误 " + jsonString); } else { if (callback != null) { try { string path = Application.persistentDataPath + "/Image"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } byte[] data = new byte[webRequest.downloadHandler.data.Length]; data = webRequest.downloadHandler.data; File.WriteAllBytes(value.mObj.localSavePath, data); } catch (Exception e) { ErrorLogPanel.Instance.Show(" 图片缓存出现错误 " + jsonString); } try { Texture2D texture2 = texture.texture; Sprite createSprite = Sprite.Create(texture2, new Rect(0, 0, texture2.width, texture2.height), Vector2.zero); // Debug.Log(webRequest.downloadHandler.data.Length + " "+ texture2.width); callback(value, createSprite); Texture2D texture3 = texture.texture; } catch (Exception e) { ErrorLogPanel.Instance.Show(" 图片生成出现错误 " + jsonString); } ////转为字节数组 // File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data); } } } } public void PostCDNVideo(DownLoadMaterial value, string jsonString, Action callback) { StartCoroutine(PostCDNRequestVideo(value, jsonString, callback)); } private IEnumerator PostCDNRequestVideo(DownLoadMaterial value, string jsonString, Action callback) { string url = value.mObj.cdnUrl; Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString)); using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) { byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); // webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(value.mObj.localSavePath, AudioType.ACC); webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerFile(value.mObj.localSavePath); webRequest.SetRequestHeader("authorization", token); foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); callback(value, null); ErrorLogPanel.Instance.Show(" 下载视频出现错误 " + jsonString); } else { callback(value, value.mObj.localSavePath); // Debug.Log("文件下载成功 :" + value.mObj.DownloadPath +" "+ webRequest.downloadHandler.data.Length); //Debug.Log(webRequest.downloadHandler.data.Length); try { //string path = Application.persistentDataPath + "/Video"; //if (!Directory.Exists(path)) //{ // Directory.CreateDirectory(path); //} //path = Application.persistentDataPath + "/StreamingAssets/Vuforia"; //if (!Directory.Exists(path)) //{ // Directory.CreateDirectory(path); //} //File.WriteAllBytes(value.mObj.localSavePath, webRequest.downloadHandler.data); } catch (Exception) { ErrorLogPanel.Instance.Show(" 视频缓存出现错误 " + jsonString); } } } } /// /// Bundle 文件 /// /// /// /// public void PostCDNBundle(DownLoadMaterial mObj, long time, string jsonString, Action CallBack) { StartCoroutine(PostCDNRequestBundle(mObj, time, jsonString, CallBack)); } private IEnumerator PostCDNRequestBundle(DownLoadMaterial mObj, long time, string jsonString, Action CallBack) { string url = mObj.mObj.cdnUrl; //using (UnityWebRequest webRequestAB = UnityWebRequestAssetBundle.GetAssetBundle(url, (uint)time, 1)) //{ // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); // // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath); // yield return webRequestAB.SendWebRequest(); // if (webRequestAB.isHttpError || webRequestAB.isNetworkError) // { // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length); // } // else // { // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB); // GameObject obj = bundle.LoadAsset(bundle.GetAllAssetNames()[0]); // List listObjs = new List(); // listObjs.Add(obj); // object value = listObjs; // CallBack(mObj, value); // } //} Dictionary headers = new Dictionary(); headers["Content-Type"] = "application/json"; headers["authorization"] = token; // webRequest.SetRequestHeader("authorization", token); //将文本转为byte数组 byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonString); Debug.Log(string.Format("url:{0} postData:{1}", url, jsonString)); //向HTTP服务器提交Post数据 WWW www = new WWW(url, bs, headers); //等待服务器的响应 yield return www; if (www.error != null) { //获取服务器的错误信息 Debug.LogError(www.error); ErrorLogPanel.Instance.Show(" 下载模型出现错误 " + jsonString); yield return null; } else { try { byte[] data = new byte[www.bytes.Length]; data = www.bytes; Debug.Log(data.Length); string path = Application.persistentDataPath + "/Model"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } File.WriteAllBytes(mObj.mObj.localSavePath, data); } catch (Exception e) { Debug.Log(e.Message.ToString()); //ErrorLogPanel.Instance.Show(" 模型缓存出现错误 " + jsonString); } try { Debug.Log(www.bytes.Length); var ab = www.assetBundle; GameObject obj = ab.LoadAsset(ab.GetAllAssetNames()[0]); List listObjs = new List(); listObjs.Add(obj); object value = listObjs; CallBack(mObj, value); } catch (Exception) { ErrorLogPanel.Instance.Show(" 模型生成出现错误 " + jsonString); } } //using (UnityWebRequest webRequestAB = new UnityWebRequest(url, "POST")) //{ // byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); // webRequestAB.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); // webRequestAB.downloadHandler = (DownloadHandler)new DownloadHandlerFile(mObj.mObj.localSavePath); // yield return webRequestAB.SendWebRequest(); // if (webRequestAB.isHttpError || webRequestAB.isNetworkError) // { // Debug.LogError(webRequestAB.error + "\n" + webRequestAB.downloadHandler.data.Length); // } // else // { // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(webRequestAB); // GameObject obj = bundle.LoadAsset(bundle.GetAllAssetNames()[0]); // List listObjs = new List(); // listObjs.Add(obj); // object value = listObjs; // CallBack(mObj, value); // } //} } public void PostTest(string Loadpath, string jsonString, Action CallBack) { StartCoroutine(PostRequest(Loadpath, jsonString, CallBack)); } // private IEnumerator PostRequest(string loadPath, string jsonString, Action CallBack) { string url = baseUrl + loadPath; Debug.Log(url); using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) { byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); webRequest.SetRequestHeader("authorization", token); foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); // Debug.Log(v.Value + " " + loadPath); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + loadPath+" "+ jsonString); GameManager.Instance.text2.text = jsonString + " " + webRequest.error; } else { // Debug.Log(webRequest.downloadHandler.text); CallBack(webRequest.downloadHandler.text); } } } public void GetAllMaterials(string methodName, string jsonString, Action CallBack) { StartCoroutine(GetRequest(methodName, jsonString, CallBack)); } private IEnumerator GetRequest(string methodName, string jsonString, Action CallBack) { string url = baseUrl + methodName; Debug.Log(url); token = jsonString; HeadAddToken(token); using (UnityWebRequest webRequest = UnityWebRequest.Get(url)) { webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); webRequest.SetRequestHeader("authorization", jsonString); foreach (var v in requestHeader) { Debug.Log(v.Key + " " + v.Value); webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); } else { Debug.Log(webRequest.downloadHandler.text); CallBack(webRequest.downloadHandler.text); } } } /// /// 登录 /// /// /// /// public void PostLogin(string methodName, string jsonString, Action CallBack) { StartCoroutine(PostRequestLogin(methodName, jsonString, CallBack)); } private IEnumerator PostRequestLogin(string methodName, string jsonString, Action CallBack) { string url = baseUrl + methodName; Debug.Log(jsonString); using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) { byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); foreach (var v in requestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text); string error = webRequest.downloadHandler.text; JObject jObject = JObject.Parse(error); CallBack(jObject["message"].ToString()); ErrorLogPanel.Instance.Show(" 请求后台数据出现错误 "); } else { Debug.Log(webRequest.downloadHandler.text); token = webRequest.downloadHandler.text; if (token.Contains("密码不正确,请重新输入") || token.Contains("用户未注册")) { CallBack(token); } else { JObject obj = JObject.Parse(token); Debug.Log(obj["data"]["token"].ToString()); token = obj["data"]["token"].ToString(); Debug.Log(token); CallBack(token); } } } } /// /// 获取本地Sprite /// /// /// public void GetLocalSprite(DownLoadMaterial mObj, Action CallBack) { // StartCoroutine(GetSpriteRequest(mObj, CallBack)); try { var pathName = mObj.mObj.localSavePath; var bytes = ReadFile(pathName); int width = Screen.width; int height = Screen.height; var texture = new Texture2D(width, height); texture.LoadImage(bytes); Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); CallBack(mObj, sprite); } catch (Exception c) { ErrorLogPanel.Instance.Show(" 加载缓存图片出现错误 " + mObj.mObj.localSavePath); Debug.LogError(c.Message); } } private IEnumerator GetSpriteRequest(DownLoadMaterial mObj, Action CallBack) { //WWW www = new WWW(mObj.mObj.localSavePath); //yield return www; //if (www.isDone && www.error == null) //{ // Texture2D texture = www.texture; // Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); // CallBack(mObj, sprite); //} //else //{ // Debug.Log(mObj.mObj.localSavePath+" " + www.error); //} UnityWebRequest request = UnityWebRequestTexture.GetTexture(mObj.mObj.localSavePath); Debug.Log(mObj.mObj.localSavePath); yield return request.SendWebRequest(); if (request.isNetworkError || request.isHttpError) { Debug.LogError(request.error); } else { Texture2D texture = DownloadHandlerTexture.GetContent(request); Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); CallBack(mObj, sprite); // LoadManager.Instance.DownLoadEnd(mObj, sprite); } } public byte[] ReadFile(string filePath) { var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); fs.Seek(0, SeekOrigin.Begin); var binary = new byte[fs.Length]; fs.Read(binary, 0, binary.Length); fs.Close(); return binary; } public void GetLocalModel(DownLoadMaterial mObj, Action CallBack) { StartCoroutine(GetModelRequest(mObj, CallBack)); } private IEnumerator GetModelRequest(DownLoadMaterial mObj, Action CallBack) { //Debug.Log(" Load LocalModel"); //UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(mObj.mObj.localSavePath); //yield return request.SendWebRequest(); //if (request.isHttpError || request.isNetworkError) //{ // Debug.LogError(request.error + "\n" + request.downloadHandler.text); // ErrorLogPanel.Instance.Show(" 加载缓存模型出现错误 " + mObj.mObj.localSavePath); //} //else //{ // try // { // Debug.Log(" Load LocalModel2"); // AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request); // GameObject obj = bundle.LoadAsset(bundle.GetAllAssetNames()[0]); // List listObjs = new List(); // listObjs.Add(obj); // LoadManager.Instance.DownLoadEnd(mObj, listObjs); // bundle.Unload(false); // object value = listObjs; // CallBack(mObj, value); // } // catch (Exception) // { // ErrorLogPanel.Instance.Show(" 生成缓存模型出现错误 " + mObj.mObj.localSavePath); // throw; // } //} AssetBundleCreateRequest request1 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(mObj.mObj.localSavePath)); yield return request1; if (false) { } else { try { Debug.Log(" Load LocalModel2"); //AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request); // AssetBundle bundle = request.downloadHandler.data; AssetBundle bundle = request1.assetBundle; GameObject obj = bundle.LoadAsset(bundle.GetAllAssetNames()[0]); List listObjs = new List(); listObjs.Add(obj); LoadManager.Instance.DownLoadEnd(mObj, listObjs); bundle.Unload(false); object value = listObjs; CallBack(mObj, value); } catch (Exception) { ErrorLogPanel.Instance.Show(" 生成缓存模型出现错误 " + mObj.mObj.localSavePath); throw; } } } public void HeadAddToken(string token) { requestHeader.Add("x-token", token); } public string GetMd5Hash(string strToEncrypt) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] bytes = Encoding.ASCII.GetBytes(strToEncrypt); byte[] encoded = md5.ComputeHash(bytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(encoded[i].ToString("x2")); } string password = sb.ToString(); password = password.Substring(0, 10); return password; } }