From c5a23e19d1b4694b04c6bf98ba3d862284700407 Mon Sep 17 00:00:00 2001 From: huyufei Date: Thu, 7 Mar 2024 16:43:37 +0800 Subject: [PATCH] =?UTF-8?q?update:=20CDNLoader=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/ExtensionKit/CDNLoader.cs | 155 +++++++++--------- 1 file changed, 77 insertions(+), 78 deletions(-) diff --git a/Runtime/GuruCore/Runtime/ExtensionKit/CDNLoader.cs b/Runtime/GuruCore/Runtime/ExtensionKit/CDNLoader.cs index 906548f..137b0e6 100644 --- a/Runtime/GuruCore/Runtime/ExtensionKit/CDNLoader.cs +++ b/Runtime/GuruCore/Runtime/ExtensionKit/CDNLoader.cs @@ -13,6 +13,13 @@ using UnityEditor; #endif +public enum LoadType +{ + Text, + Bytes, + Texture2D, +} + /// /// CDN云控配置 /// @@ -21,6 +28,7 @@ public class CDNConfig { internal static string CdnConfigRemoteKey = "cdn_config"; + public bool enable = true; public string[] replace; public string main; public string fallback; @@ -234,59 +242,22 @@ public class CDNLoader : MonoBehaviour /// /// /// - /// + /// 多条加载的时候请给 ID 值 /// - public void LoadText(string url, Action onComplete, Action onFail, Action onProgress = null) + public void LoadAsset(string url, LoadType type = LoadType.Text, string id = "", + Action onComplete = null, Action onProgress = null) { + if (string.IsNullOrEmpty(id)) id = url; // id 为空的情况下, 使用 URL 作为唯一标识 AddTask(new LoadTask() { + id = id, url = url, - onGetString = onComplete, - onFail = onFail, + onComplete = onComplete, onProgress = onProgress, - type = LoadType.Text + type = type, }); } - /// - /// 加载文本 - /// - /// - /// - /// - /// - public void LoadFile(string url, string fileStr, Action onComplete, Action onFail, Action onProgress = null) - { - AddTask(new LoadTask() - { - url = url, - onGetBytes = onComplete, - onFail = onFail, - onProgress = onProgress, - type = LoadType.Bytes, - fileName = fileStr - }); - } - - /// - /// 加载文本 - /// - /// - /// - /// - /// - public void LoadTexture2D(string url, Action onComplete, Action onFail, Action onProgress = null) - { - AddTask(new LoadTask() - { - url = url, - onGetTexture = onComplete, - onFail = onFail, - onProgress = onProgress, - type = LoadType.Bytes - }); - } - #endregion #region 加载队列 @@ -320,7 +291,6 @@ public class CDNLoader : MonoBehaviour Debug.Log($"--- 下载地址: { task.fixedUrl }"); // Debug.Log($"--- _useFallback: {_useFallback}"); // Debug.Log($"--- _retryNum: {_retryNum}"); - // 使用Using初始化加载器 using (UnityWebRequest www = UnityWebRequest.Get(task.fixedUrl)) { @@ -331,25 +301,30 @@ public class CDNLoader : MonoBehaviour www.timeout = _config.timeout; yield return www.SendWebRequest(); - + + var result = LoadResult.Create(task); if (www.result == UnityWebRequest.Result.Success) { - switch (task.type) + bool success = www.downloadHandler != null; + result.success = success; + if (success) { - case LoadType.Text: - task.onGetString?.Invoke(www.downloadHandler.text, task.fileName); - break; - case LoadType.Bytes: - task.onGetBytes?.Invoke(www.url, www.downloadHandler.data, task.fileName); - break; - case LoadType.Texture2D: - var handler2D = www.downloadHandler as DownloadHandlerTexture; - task.onGetTexture?.Invoke(handler2D?.texture ? handler2D.texture : null); - break; - default: - //TODO 请扩展更多的加载接口 - break; + switch (task.type) + { + + case LoadType.Bytes: + result.data = www.downloadHandler.data; + break; + case LoadType.Texture2D: + var handler2D = www.downloadHandler as DownloadHandlerTexture; + result.texture = (handler2D?.texture ? handler2D?.texture : null); + break; + default: + result.text = www.downloadHandler.text; + break; + } } + task.onComplete?.Invoke(result); } else { @@ -362,7 +337,9 @@ public class CDNLoader : MonoBehaviour { if (task.useFallback) { - task.onFail(www.error); + result.success = false; + result.error = www.error; + task.onComplete?.Invoke(result); task.Dispose(); } else @@ -384,7 +361,6 @@ public class CDNLoader : MonoBehaviour } } - #endregion #region 单元测试 @@ -407,13 +383,41 @@ public class CDNLoader : MonoBehaviour #endregion -} + #region 加载结果 -public enum LoadType -{ - Text, - Bytes, - Texture2D, + /// + /// 加载结果 + /// + public class LoadResult + { + public string id; + public LoadType type; + public bool success = false; + public string url; + public string error; + public string text = ""; + public byte[] data = null; + public Texture2D texture = null; + public AssetBundle assetBundle = null; + + + internal static LoadResult Create(LoadTask task, bool success = false, string error = "") + { + var result = new LoadResult() + { + id = task.id, + type = task.type, + success = success, + url = task.fixedUrl, + error = error + }; + return result; + } + + + } + + #endregion } /// @@ -424,11 +428,8 @@ internal class LoadTask { public string url; public string fixedUrl; - public string fileName; - public Action onGetString; - public Action onGetBytes; - public Action onGetTexture; - public Action onFail; + public string id; + public Action onComplete; public Action onProgress; public bool useFallback = false; public int retryNum = 0; @@ -437,10 +438,8 @@ internal class LoadTask public void Dispose() { - onGetString = null; - onGetBytes = null; - onGetTexture = null; - onFail = null; + onComplete = null; onProgress = null; } -} \ No newline at end of file +} +