update: 更新和完善 CDNLoader 的逻辑

deeplink
胡宇飞 2024-03-07 11:17:21 +08:00
parent e313c9af80
commit 076f784b1c
1 changed files with 18 additions and 17 deletions

View File

@ -19,7 +19,7 @@ using UnityEditor;
[Serializable]
public class CDNConfig
{
public const string KEY_CDN_CONFIG = "cdn_config";
internal static string CdnConfigRemoteKey = "cdn_config";
public string[] replace;
public string main;
@ -28,11 +28,7 @@ public class CDNConfig
public int timeout;
private static CDNConfig _config;
public static void Init(string defaultValue)
{
FirebaseUtil.AppendDefaultValue(KEY_CDN_CONFIG, defaultValue);
}
/// <summary>
/// 加载配置
@ -42,10 +38,10 @@ public class CDNConfig
{
if (_config != null)
return _config;
try
{
_config = FirebaseUtil.GetRemoteConfig<CDNConfig>(KEY_CDN_CONFIG);
_config = FirebaseUtil.GetRemoteConfig<CDNConfig>(CdnConfigRemoteKey);
}
catch (Exception e)
{
@ -91,8 +87,11 @@ public class CDNLoader : MonoBehaviour
/// 创建Loader
/// </summary>
/// <returns></returns>
public static CDNLoader Create()
public static CDNLoader Create(string remoteKey = "")
{
// 注入自定义的 CDNConfigKey, 可读取自定义的Default配置
if(!string.IsNullOrEmpty(remoteKey)) CDNConfig.CdnConfigRemoteKey = remoteKey;
var go = new GameObject(nameof(CDNLoader));
var loader = go.AddComponent<CDNLoader>();
return loader;
@ -237,7 +236,7 @@ public class CDNLoader : MonoBehaviour
/// <param name="onComplete"></param>
/// <param name="onFail"></param>
/// <param name="onProgress"></param>
public void LoadText(string url, Action<string> onComplete, Action<string> onFail, Action<float> onProgress = null)
public void LoadText(string url, Action<string, string> onComplete, Action<string> onFail, Action<float> onProgress = null)
{
AddTask(new LoadTask()
{
@ -256,7 +255,7 @@ public class CDNLoader : MonoBehaviour
/// <param name="onComplete"></param>
/// <param name="onFail"></param>
/// <param name="onProgress"></param>
public void LoadFile(string url, Action<byte[]> onComplete, Action<string> onFail, Action<float> onProgress = null)
public void LoadFile(string url, string fileStr, Action<string, byte[], string> onComplete, Action<string> onFail, Action<float> onProgress = null)
{
AddTask(new LoadTask()
{
@ -264,7 +263,8 @@ public class CDNLoader : MonoBehaviour
onGetBytes = onComplete,
onFail = onFail,
onProgress = onProgress,
type = LoadType.Bytes
type = LoadType.Bytes,
fileName = fileStr
});
}
@ -337,10 +337,10 @@ public class CDNLoader : MonoBehaviour
switch (task.type)
{
case LoadType.Text:
task.onGetString?.Invoke(www.downloadHandler.text);
task.onGetString?.Invoke(www.downloadHandler.text, task.fileName);
break;
case LoadType.Bytes:
task.onGetBytes?.Invoke(www.downloadHandler.data);
task.onGetBytes?.Invoke(www.url, www.downloadHandler.data, task.fileName);
break;
case LoadType.Texture2D:
var handler2D = www.downloadHandler as DownloadHandlerTexture;
@ -391,7 +391,7 @@ public class CDNLoader : MonoBehaviour
#if UNITY_EDITOR
[MenuItem("Test/Test CDNLoader...")]
// [MenuItem("Test/Test CDNLoader...")]
public static void EditorTestGetURL()
{
string url = "https://cdn.dof.fungame.studio/PicAssets%2FAndroid%2F6_cx_0818_17?generation=1649423811796255&alt=media";
@ -424,8 +424,9 @@ internal class LoadTask
{
public string url;
public string fixedUrl;
public Action<string> onGetString;
public Action<byte[]> onGetBytes;
public string fileName;
public Action<string, string> onGetString;
public Action<string, byte[], string> onGetBytes;
public Action<Texture2D> onGetTexture;
public Action<string> onFail;
public Action<float> onProgress;