com.alicizax.unity.tuyoogam.../Samples~/Mini Game/Runtime/TiktokFileSystem/Operation/internal/LoadTiktokAssetBundleOperation.cs

114 lines
3.7 KiB
C#
Raw Permalink Normal View History

2025-09-02 19:21:49 +08:00
#if UNITY_WEBGL && DOUYINMINIGAME
using UnityEngine;
namespace YooAsset
{
internal class LoadTiktokAssetBundleOperation : LoadWebAssetBundleOperation
{
protected enum ESteps
{
None,
CreateRequest,
CheckRequest,
TryAgain,
Done,
}
private readonly PackageBundle _bundle;
private readonly DownloadFileOptions _options;
2025-09-10 16:04:39 +08:00
private UnityTiktokAssetBundleRequestOperation _unityAssetBundleRequestOp;
2025-09-02 19:21:49 +08:00
private int _requestCount = 0;
private float _tryAgainTimer;
private int _failedTryAgain;
private ESteps _steps = ESteps.None;
internal LoadTiktokAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options)
{
_bundle = bundle;
_options = options;
}
internal override void InternalStart()
{
_steps = ESteps.CreateRequest;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
// 创建下载器
if (_steps == ESteps.CreateRequest)
{
string url = GetRequestURL();
2025-09-10 16:04:39 +08:00
_unityAssetBundleRequestOp = new UnityTiktokAssetBundleRequestOperation(_bundle, url);
_unityAssetBundleRequestOp.StartOperation();
AddChildOperation(_unityAssetBundleRequestOp);
2025-09-02 19:21:49 +08:00
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
2025-09-10 16:04:39 +08:00
_unityAssetBundleRequestOp.UpdateOperation();
Progress = _unityAssetBundleRequestOp.Progress;
DownloadProgress = _unityAssetBundleRequestOp.DownloadProgress;
DownloadedBytes = _unityAssetBundleRequestOp.DownloadedBytes;
if (_unityAssetBundleRequestOp.IsDone == false)
2025-09-02 19:21:49 +08:00
return;
2025-09-10 16:04:39 +08:00
if (_unityAssetBundleRequestOp.Status == EOperationStatus.Succeed)
2025-09-02 19:21:49 +08:00
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
2025-09-10 16:04:39 +08:00
Result = _unityAssetBundleRequestOp.Result;
2025-09-02 19:21:49 +08:00
}
else
{
if (_failedTryAgain > 0)
{
_steps = ESteps.TryAgain;
2025-09-10 16:04:39 +08:00
YooLogger.Warning($"Failed download : {_unityAssetBundleRequestOp.URL} Try again !");
2025-09-02 19:21:49 +08:00
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
2025-09-10 16:04:39 +08:00
Error = _unityAssetBundleRequestOp.Error;
2025-09-02 19:21:49 +08:00
YooLogger.Error(Error);
}
}
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 1f)
{
_tryAgainTimer = 0f;
_failedTryAgain--;
Progress = 0f;
DownloadProgress = 0f;
DownloadedBytes = 0;
_steps = ESteps.CreateRequest;
}
}
}
/// <summary>
/// 获取网络请求地址
/// </summary>
private string GetRequestURL()
{
// 轮流返回请求地址
_requestCount++;
if (_requestCount % 2 == 0)
return _options.FallbackURL;
else
return _options.MainURL;
}
}
}
#endif