update: 更新和完善 CDNLoader 的逻辑
parent
e313c9af80
commit
076f784b1c
|
|
@ -19,7 +19,7 @@ using UnityEditor;
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class CDNConfig
|
public class CDNConfig
|
||||||
{
|
{
|
||||||
public const string KEY_CDN_CONFIG = "cdn_config";
|
internal static string CdnConfigRemoteKey = "cdn_config";
|
||||||
|
|
||||||
public string[] replace;
|
public string[] replace;
|
||||||
public string main;
|
public string main;
|
||||||
|
|
@ -28,11 +28,7 @@ public class CDNConfig
|
||||||
public int timeout;
|
public int timeout;
|
||||||
|
|
||||||
private static CDNConfig _config;
|
private static CDNConfig _config;
|
||||||
|
|
||||||
public static void Init(string defaultValue)
|
|
||||||
{
|
|
||||||
FirebaseUtil.AppendDefaultValue(KEY_CDN_CONFIG, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 加载配置
|
/// 加载配置
|
||||||
|
|
@ -42,10 +38,10 @@ public class CDNConfig
|
||||||
{
|
{
|
||||||
if (_config != null)
|
if (_config != null)
|
||||||
return _config;
|
return _config;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_config = FirebaseUtil.GetRemoteConfig<CDNConfig>(KEY_CDN_CONFIG);
|
_config = FirebaseUtil.GetRemoteConfig<CDNConfig>(CdnConfigRemoteKey);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
@ -91,8 +87,11 @@ public class CDNLoader : MonoBehaviour
|
||||||
/// 创建Loader
|
/// 创建Loader
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <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 go = new GameObject(nameof(CDNLoader));
|
||||||
var loader = go.AddComponent<CDNLoader>();
|
var loader = go.AddComponent<CDNLoader>();
|
||||||
return loader;
|
return loader;
|
||||||
|
|
@ -237,7 +236,7 @@ public class CDNLoader : MonoBehaviour
|
||||||
/// <param name="onComplete"></param>
|
/// <param name="onComplete"></param>
|
||||||
/// <param name="onFail"></param>
|
/// <param name="onFail"></param>
|
||||||
/// <param name="onProgress"></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()
|
AddTask(new LoadTask()
|
||||||
{
|
{
|
||||||
|
|
@ -256,7 +255,7 @@ public class CDNLoader : MonoBehaviour
|
||||||
/// <param name="onComplete"></param>
|
/// <param name="onComplete"></param>
|
||||||
/// <param name="onFail"></param>
|
/// <param name="onFail"></param>
|
||||||
/// <param name="onProgress"></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()
|
AddTask(new LoadTask()
|
||||||
{
|
{
|
||||||
|
|
@ -264,7 +263,8 @@ public class CDNLoader : MonoBehaviour
|
||||||
onGetBytes = onComplete,
|
onGetBytes = onComplete,
|
||||||
onFail = onFail,
|
onFail = onFail,
|
||||||
onProgress = onProgress,
|
onProgress = onProgress,
|
||||||
type = LoadType.Bytes
|
type = LoadType.Bytes,
|
||||||
|
fileName = fileStr
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -337,10 +337,10 @@ public class CDNLoader : MonoBehaviour
|
||||||
switch (task.type)
|
switch (task.type)
|
||||||
{
|
{
|
||||||
case LoadType.Text:
|
case LoadType.Text:
|
||||||
task.onGetString?.Invoke(www.downloadHandler.text);
|
task.onGetString?.Invoke(www.downloadHandler.text, task.fileName);
|
||||||
break;
|
break;
|
||||||
case LoadType.Bytes:
|
case LoadType.Bytes:
|
||||||
task.onGetBytes?.Invoke(www.downloadHandler.data);
|
task.onGetBytes?.Invoke(www.url, www.downloadHandler.data, task.fileName);
|
||||||
break;
|
break;
|
||||||
case LoadType.Texture2D:
|
case LoadType.Texture2D:
|
||||||
var handler2D = www.downloadHandler as DownloadHandlerTexture;
|
var handler2D = www.downloadHandler as DownloadHandlerTexture;
|
||||||
|
|
@ -391,7 +391,7 @@ public class CDNLoader : MonoBehaviour
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
|
|
||||||
[MenuItem("Test/Test CDNLoader...")]
|
// [MenuItem("Test/Test CDNLoader...")]
|
||||||
public static void EditorTestGetURL()
|
public static void EditorTestGetURL()
|
||||||
{
|
{
|
||||||
string url = "https://cdn.dof.fungame.studio/PicAssets%2FAndroid%2F6_cx_0818_17?generation=1649423811796255&alt=media";
|
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 url;
|
||||||
public string fixedUrl;
|
public string fixedUrl;
|
||||||
public Action<string> onGetString;
|
public string fileName;
|
||||||
public Action<byte[]> onGetBytes;
|
public Action<string, string> onGetString;
|
||||||
|
public Action<string, byte[], string> onGetBytes;
|
||||||
public Action<Texture2D> onGetTexture;
|
public Action<Texture2D> onGetTexture;
|
||||||
public Action<string> onFail;
|
public Action<string> onFail;
|
||||||
public Action<float> onProgress;
|
public Action<float> onProgress;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue