From f7b85619f14b015d40af369b67b3ac5b4cf9b678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E8=88=B9=E6=A0=9E=E4=BB=94?= Date: Fri, 14 Apr 2023 21:08:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Logic/CardManager.cs | 3 ++- Assets/Scripts/Logic/Data/Card.cs | 2 +- Assets/Scripts/Logic/DownloadData.cs | 13 ++++++++++--- Assets/Scripts/Logic/DownloadImage.cs | 9 +++++++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Assets/Scripts/Logic/CardManager.cs b/Assets/Scripts/Logic/CardManager.cs index a4b4c149..0a72bc53 100644 --- a/Assets/Scripts/Logic/CardManager.cs +++ b/Assets/Scripts/Logic/CardManager.cs @@ -63,8 +63,9 @@ public class CardManager : SingletonMono json = tx.text; } else - //读取系列记录存档 + { //读取系列记录存档 json = File.ReadAllText(SeriesListPath); + } LoadFromJsonText(json); } diff --git a/Assets/Scripts/Logic/Data/Card.cs b/Assets/Scripts/Logic/Data/Card.cs index c0c8dce9..a987effc 100644 --- a/Assets/Scripts/Logic/Data/Card.cs +++ b/Assets/Scripts/Logic/Data/Card.cs @@ -112,7 +112,7 @@ public class Card json += $"\t\"Name\":\"{Name}\",\n"; json += $"\t\"Series\":\"{Series}\",\n"; json += $"\t\"Num\":\"{Num}\",\n"; - json += $"\t\"Info\":\"{Info}\",\n"; + json += $"\t\"Info\":\"{Info.Replace("\n","\\n")}\",\n"; json += $"\t\"Rare\":\"{Rare}\",\n"; json += $"\t\"Box\":\"{Box}\",\n"; json += $"\t\"Cost\":{Cost},\n"; diff --git a/Assets/Scripts/Logic/DownloadData.cs b/Assets/Scripts/Logic/DownloadData.cs index fb7d8b69..63aab222 100644 --- a/Assets/Scripts/Logic/DownloadData.cs +++ b/Assets/Scripts/Logic/DownloadData.cs @@ -148,7 +148,7 @@ public class DownloadData : SingletonMono } - IEnumerator DownloadText(string url, Action success = null, Action fail = null) + IEnumerator DownloadText(string url, Action success = null, Action fail = null, int retry = 0) { // 创建一个 UnityWebRequest 对象,并指定要下载的网页 URL @@ -160,8 +160,15 @@ public class DownloadData : SingletonMono // 检查是否有错误发生 if (webRequest.result != UnityWebRequest.Result.Success) { - Debug.LogError("网络请求失败: " + webRequest.error + "\n" + url); - fail?.Invoke(webRequest.error); + Debug.LogError("网络请求失败: " + webRequest.error +":" + retry + "\n" + url); + + if (retry < 5) + { + Debug.Log($"重新尝试:{url} - 次数{retry}"); + yield return StartCoroutine(DownloadText(url, success, fail, retry + 1)); + } + else + fail?.Invoke(webRequest.error); yield break; } diff --git a/Assets/Scripts/Logic/DownloadImage.cs b/Assets/Scripts/Logic/DownloadImage.cs index 02a4051e..719f8e3c 100644 --- a/Assets/Scripts/Logic/DownloadImage.cs +++ b/Assets/Scripts/Logic/DownloadImage.cs @@ -66,7 +66,7 @@ public class DownlaodImage : SingletonMono return $"{path}/{card.Num.Replace("/", "_")}.png"; } - + /// /// 保存文件到本地 @@ -165,7 +165,7 @@ public class DownlaodImage : SingletonMono downloading = false; } - IEnumerator DownloadingImg(Card card) + IEnumerator DownloadingImg(Card card, int retry = 0) { // 创建一个 UnityWebRequest 对象,并指定要下载的网页 URL UnityWebRequest webRequest = UnityWebRequest.Get(card.ImgUrl); @@ -177,6 +177,11 @@ public class DownlaodImage : SingletonMono if (webRequest.result != UnityWebRequest.Result.Success) { Debug.LogError("网络请求失败: " + webRequest.error + "\n" + card.ImgUrl); + if (retry < 5) + { + Debug.Log($"重新尝试:{card.Num} - 次数{retry}"); + yield return StartCoroutine(DownloadingImg(card, retry + 1)); + } yield break; } byte[] bytes = webRequest.downloadHandler.data; -- GitLab