using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Threading.Tasks; using ET; using FairyGUI; using UnityEngine; using UnityEngine.Networking; namespace GFGGame { public static class MatchingPhotoHelper { //将图片上传到华为云 public static async Task PushToHWCloud(string signUrl, byte[] buffer) { HttpContent content = new ByteArrayContent(buffer); HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseCookies = false }); HttpResponseMessage response = await httpClient.PutAsync(signUrl, content); response.EnsureSuccessStatusCode(); await response.Content.ReadAsStreamAsync(); if (response.StatusCode != HttpStatusCode.OK) { ViewManager.Hide(); PromptController.Instance.ShowFloatTextPrompt("上传资源失败"); return false; } else { return true; } } public static IEnumerator Download(List list) { for (int i = 0; i < list.Count; i++) { MatchingPhotoWorksData data = list[i]; if (data == null || data.Ntexture != null) continue; int count = 0; yield return DownloadPicture(data, count); } ET.Log.Debug("Download finish!!!"); EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH); } public static IEnumerator Download(List list) { for (int i = 0; i < list.Count; i++) { MatchingWorksData data = list[i]; if (data == null || data.Ntexture != null) continue; int count = 0; yield return DownloadPicture(data, count); } ET.Log.Debug("Download finish!!!"); EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH); } public static IEnumerator Download(MatchingPhotoWorksData list) { MatchingPhotoWorksData data = list; yield return DownloadPicture(data, 1); ET.Log.Debug("Download finish!!!"); EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH); } private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count) { if (count >= 3) { PromptController.Instance.ShowFloatTextPrompt("下载失败"); ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data)); data.Ntexture = null; ViewManager.Hide(); yield break; } using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.JudgingInfo.PictureTempUrl)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data)); count += 1; yield return DownloadPicture(data, count); } else { Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture; data.Ntexture = new NTexture(texture); data.Bytes = texture.EncodeToJPG(); } } } private static IEnumerator DownloadPicture(MatchingWorksData data, int count) { if (count >= 3) { PromptController.Instance.ShowFloatTextPrompt("下载失败"); ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data)); data.Ntexture = null; ViewManager.Hide(); yield break; } using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.WorksInfo.PictureTempUrl)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError) { ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data)); count += 1; yield return DownloadPicture(data, count); } else { Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture; data.Ntexture = new NTexture(texture); data.Bytes = texture.EncodeToJPG(); } } } } }