com.alicizax.unity.tuyoogam.../Runtime/DownloadSystem/Operation/Internal/UnityAssetBundleRequestOperation.cs

113 lines
3.8 KiB
C#
Raw Normal View History

2025-09-02 19:21:49 +08:00
using UnityEngine.Networking;
using UnityEngine;
2025-01-09 11:31:04 +08:00
namespace YooAsset
{
2025-09-02 19:21:49 +08:00
internal class UnityAssetBundleRequestOperation : UnityWebRequestOperation
2025-01-09 11:31:04 +08:00
{
2025-09-02 19:21:49 +08:00
protected enum ESteps
{
None,
CreateRequest,
Download,
Done,
}
private UnityWebRequestAsyncOperation _requestOperation;
2025-01-09 11:31:04 +08:00
private DownloadHandlerAssetBundle _downloadhandler;
2025-09-02 19:21:49 +08:00
private readonly PackageBundle _packageBundle;
private readonly bool _disableUnityWebCache;
2025-01-09 11:31:04 +08:00
private ESteps _steps = ESteps.None;
2025-09-02 19:21:49 +08:00
/// <summary>
/// 请求结果
/// </summary>
public AssetBundle Result { private set; get; }
internal UnityAssetBundleRequestOperation(PackageBundle packageBundle, bool disableUnityWebCache, string url) : base(url)
2025-01-09 11:31:04 +08:00
{
2025-09-02 19:21:49 +08:00
_packageBundle = packageBundle;
2025-01-09 11:31:04 +08:00
_disableUnityWebCache = disableUnityWebCache;
}
2025-02-28 16:11:01 +08:00
internal override void InternalStart()
2025-01-09 11:31:04 +08:00
{
_steps = ESteps.CreateRequest;
}
2025-02-28 16:11:01 +08:00
internal override void InternalUpdate()
2025-01-09 11:31:04 +08:00
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CreateRequest)
{
CreateWebRequest();
2025-09-02 19:21:49 +08:00
_steps = ESteps.Download;
2025-01-09 11:31:04 +08:00
}
2025-09-02 19:21:49 +08:00
if (_steps == ESteps.Download)
2025-01-09 11:31:04 +08:00
{
DownloadProgress = _webRequest.downloadProgress;
DownloadedBytes = (long)_webRequest.downloadedBytes;
2025-09-02 19:21:49 +08:00
Progress = _requestOperation.progress;
if (_requestOperation.isDone == false)
2025-01-09 11:31:04 +08:00
return;
if (CheckRequestResult())
{
2025-02-28 16:11:01 +08:00
AssetBundle assetBundle = _downloadhandler.assetBundle;
2025-01-09 11:31:04 +08:00
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
2025-09-02 19:21:49 +08:00
Error = $"URL : {_requestURL} Download handler asset bundle object is null !";
2025-01-09 11:31:04 +08:00
}
else
{
_steps = ESteps.Done;
Result = assetBundle;
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.Done;
2025-09-02 19:21:49 +08:00
Status = EOperationStatus.Failed;
2025-01-09 11:31:04 +08:00
}
2025-09-02 19:21:49 +08:00
// 注意:最终释放请求器
DisposeRequest();
2025-01-09 11:31:04 +08:00
}
}
private void CreateWebRequest()
{
2025-02-28 16:11:01 +08:00
_downloadhandler = CreateWebDownloadHandler();
2025-01-09 11:31:04 +08:00
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true;
2025-09-02 19:21:49 +08:00
_requestOperation = _webRequest.SendWebRequest();
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
private DownloadHandlerAssetBundle CreateWebDownloadHandler()
2025-01-09 11:31:04 +08:00
{
if (_disableUnityWebCache)
{
2025-09-02 19:21:49 +08:00
var downloadhandler = new DownloadHandlerAssetBundle(_requestURL, _packageBundle.UnityCRC);
2025-01-09 11:31:04 +08:00
#if UNITY_2020_3_OR_NEWER
downloadhandler.autoLoadAssetBundle = false;
#endif
return downloadhandler;
}
else
{
// 注意:优先从浏览器缓存里获取文件
// The file hash defining the version of the asset bundle.
2025-09-02 19:21:49 +08:00
Hash128 fileHash = Hash128.Parse(_packageBundle.FileHash);
var downloadhandler = new DownloadHandlerAssetBundle(_requestURL, fileHash, _packageBundle.UnityCRC);
2025-01-09 11:31:04 +08:00
#if UNITY_2020_3_OR_NEWER
downloadhandler.autoLoadAssetBundle = false;
#endif
return downloadhandler;
}
}
}
}