Initial commit

This commit is contained in:
hevinci
2022-03-01 10:44:12 +08:00
parent 1d54164bfb
commit e66853fd46
237 changed files with 14462 additions and 2 deletions
@@ -0,0 +1,146 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using YooAsset.Utility;
namespace YooAsset
{
/// <summary>
/// 1. 保证每一时刻资源文件只存在一个下载器
/// 2. 保证下载器下载完成后立刻验证并缓存
/// 3. 保证资源文件不会被重复下载
/// </summary>
internal static class DownloadSystem
{
private static readonly Dictionary<string, FileDownloader> _downloaderDic = new Dictionary<string, FileDownloader>();
private static readonly List<string> _removeList = new List<string>(100);
private static readonly Dictionary<string, string> _cachedHashList = new Dictionary<string, string>(1000);
/// <summary>
/// 更新所有下载器
/// </summary>
public static void Update()
{
// 更新下载器
_removeList.Clear();
foreach (var valuePair in _downloaderDic)
{
var downloader = valuePair.Value;
downloader.Update();
if (downloader.IsDone())
_removeList.Add(valuePair.Key);
}
// 移除下载器
foreach (var key in _removeList)
{
_downloaderDic.Remove(key);
}
}
/// <summary>
/// 开始下载资源文件
/// 注意:只有第一次请求的参数才是有效的
/// </summary>
public static FileDownloader BeginDownload(AssetBundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
{
// 查询存在的下载器
if (_downloaderDic.TryGetValue(bundleInfo.Hash, out var downloader))
{
return downloader;
}
// 如果资源已经缓存
if(ContainsVerifyFile(bundleInfo.Hash))
{
var newDownloader = new FileDownloader(bundleInfo);
newDownloader.SetDone();
return newDownloader;
}
// 创建新的下载器
{
Logger.Log($"Beginning to download file : {bundleInfo.BundleName} URL : {bundleInfo.RemoteMainURL}");
FileUtility.CreateFileDirectory(bundleInfo.LocalPath);
var newDownloader = new FileDownloader(bundleInfo);
newDownloader.SendRequest(failedTryAgain, timeout);
_downloaderDic.Add(bundleInfo.Hash, newDownloader);
return newDownloader;
}
}
/// <summary>
/// 获取下载器的总数
/// </summary>
public static int GetDownloaderTotalCount()
{
return _downloaderDic.Count;
}
/// <summary>
/// 查询是否为验证文件
/// 注意:被收录的文件完整性是绝对有效的
/// </summary>
public static bool ContainsVerifyFile(string hash)
{
if (_cachedHashList.ContainsKey(hash))
{
string filePath = PatchHelper.MakeSandboxCacheFilePath(hash);
if (File.Exists(filePath))
{
return true;
}
else
{
string bundleName = _cachedHashList[hash];
_cachedHashList.Remove(hash);
Logger.Error($"Cache file is missing : {bundleName} Hash : {hash}");
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 缓存验证过的文件
/// </summary>
public static void CacheVerifyFile(string hash, string bundleName)
{
if (_cachedHashList.ContainsKey(hash) == false)
{
Logger.Log($"Cache verify file : {bundleName} Hash : {hash}");
_cachedHashList.Add(hash, bundleName);
}
}
// 验证文件完整性
public static bool CheckContentIntegrity(AssetBundleInfo bundleInfo)
{
return CheckContentIntegrity(bundleInfo.LocalPath, bundleInfo.SizeBytes, bundleInfo.CRC);
}
public static bool CheckContentIntegrity(PatchBundle patchBundle)
{
string filePath = PatchHelper.MakeSandboxCacheFilePath(patchBundle.Hash);
return CheckContentIntegrity(filePath, patchBundle.SizeBytes, patchBundle.CRC);
}
public static bool CheckContentIntegrity(string filePath, long size, string crc)
{
if (File.Exists(filePath) == false)
return false;
// 先验证文件大小
long fileSize = FileUtility.GetFileSize(filePath);
if (fileSize != size)
return false;
// 再验证文件CRC
string fileCRC = HashUtility.FileCRC32(filePath);
return fileCRC == crc;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d19eae1f43ecf6347925a06730a6c846
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,14 @@
namespace YooAsset
{
/// <summary>
/// 下载状态
/// </summary>
public enum EDownloaderStates
{
None,
Loading,
Failed,
Succeed,
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cb052d4c6ef18f54d9407cb48eafdfb9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,234 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
internal class FileDownloader
{
private enum ESteps
{
None,
CreateDownload,
CheckDownload,
TryAgain,
Succeed,
Failed,
}
public AssetBundleInfo BundleInfo { private set; get; }
private UnityWebRequest _webRequest;
private UnityWebRequestAsyncOperation _operationHandle;
private ESteps _steps = ESteps.None;
private string _lastError = string.Empty;
private int _timeout;
private int _failedTryAgain;
private int _requestCount;
private string _requestURL;
// 重置变量
private bool _isAbort = false;
private ulong _latestDownloadBytes;
private float _latestDownloadRealtime;
private float _tryAgainTimer;
/// <summary>
/// 下载进度(0-100f)
/// </summary>
public float DownloadProgress { private set; get; }
/// <summary>
/// 已经下载的总字节数
/// </summary>
public ulong DownloadedBytes { private set; get; }
internal FileDownloader(AssetBundleInfo bundleInfo)
{
BundleInfo = bundleInfo;
}
internal void SendRequest(int failedTryAgain, int timeout)
{
if (string.IsNullOrEmpty(BundleInfo.LocalPath))
throw new ArgumentNullException();
if (_steps == ESteps.None)
{
_failedTryAgain = failedTryAgain;
_timeout = timeout;
_steps = ESteps.CreateDownload;
}
}
internal void Update()
{
if (_steps == ESteps.None)
return;
if (_steps == ESteps.Failed || _steps == ESteps.Succeed)
return;
// 创建下载器
if (_steps == ESteps.CreateDownload)
{
// 重置变量
DownloadProgress = 0f;
DownloadedBytes = 0;
_isAbort = false;
_latestDownloadBytes = 0;
_latestDownloadRealtime = Time.realtimeSinceStartup;
_tryAgainTimer = 0f;
_requestCount++;
_requestURL = GetRequestURL();
_webRequest = new UnityWebRequest(_requestURL, UnityWebRequest.kHttpVerbGET);
DownloadHandlerFile handler = new DownloadHandlerFile(BundleInfo.LocalPath);
handler.removeFileOnAbort = true;
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_operationHandle = _webRequest.SendWebRequest();
_steps = ESteps.CheckDownload;
}
// 检测下载结果
if (_steps == ESteps.CheckDownload)
{
DownloadProgress = _webRequest.downloadProgress * 100f;
DownloadedBytes = _webRequest.downloadedBytes;
if (_operationHandle.isDone == false)
{
CheckTimeout();
return;
}
// 检查网络错误
bool isError = false;
#if UNITY_2020_3_OR_NEWER
if (_webRequest.result != UnityWebRequest.Result.Success)
{
isError = true;
_lastError = _webRequest.error;
}
#else
if (_webRequest.isNetworkError || _webRequest.isHttpError)
{
isError = true;
_lastError = _webRequest.error;
}
#endif
// 检查文件完整性
if (isError == false)
{
// 注意:如果文件验证失败需要删除文件
if (DownloadSystem.CheckContentIntegrity(BundleInfo) == false)
{
isError = true;
_lastError = $"Verification failed";
if (File.Exists(BundleInfo.LocalPath))
File.Delete(BundleInfo.LocalPath);
}
}
if (isError)
{
ReportError();
if (_failedTryAgain > 0)
_steps = ESteps.TryAgain;
else
_steps = ESteps.Failed;
}
else
{
_steps = ESteps.Succeed;
DownloadSystem.CacheVerifyFile(BundleInfo.Hash, BundleInfo.BundleName);
}
// 释放下载器
DisposeWebRequest();
}
// 重新尝试下载
if (_steps == ESteps.TryAgain)
{
_tryAgainTimer += Time.unscaledDeltaTime;
if (_tryAgainTimer > 0.5f)
{
_failedTryAgain--;
_steps = ESteps.CreateDownload;
Logger.Warning($"Try again download : {_requestURL}");
}
}
}
internal void SetDone()
{
_steps = ESteps.Succeed;
}
private string GetRequestURL()
{
// 轮流返回请求地址
if (_requestCount % 2 == 0)
return BundleInfo.RemoteFallbackURL;
else
return BundleInfo.RemoteMainURL;
}
private void CheckTimeout()
{
// 注意:在连续时间段内无新增下载数据及判定为超时
if (_isAbort == false)
{
if (_latestDownloadBytes != DownloadedBytes)
{
_latestDownloadBytes = DownloadedBytes;
_latestDownloadRealtime = Time.realtimeSinceStartup;
}
float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
if (offset > _timeout)
{
Logger.Warning($"Web file request timeout : {_requestURL}");
_webRequest.Abort();
_isAbort = true;
}
}
}
private void DisposeWebRequest()
{
if (_webRequest != null)
{
_webRequest.Dispose();
_webRequest = null;
_operationHandle = null;
}
}
/// <summary>
/// 检测下载器是否已经完成(无论成功或失败)
/// </summary>
public bool IsDone()
{
return _steps == ESteps.Succeed || _steps == ESteps.Failed;
}
/// <summary>
/// 下载过程是否发生错误
/// </summary>
/// <returns></returns>
public bool HasError()
{
return _steps == ESteps.Failed;
}
/// <summary>
/// 报告错误信息
/// </summary>
public void ReportError()
{
Logger.Error($"Failed to download : {_requestURL} Error : {_lastError}");
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4c6e1a8bc8d5e664395395daa772ddd7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,252 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Threading;
namespace YooAsset
{
internal class HttpDownloader
{
private enum ESteps
{
None,
CreateDownload,
CheckDownload,
Succeed,
Failed,
}
public AssetBundleInfo BundleInfo { private set; get; }
private ESteps _steps = ESteps.None;
// 线程
private bool _threadOver = false;
private bool _threadResult = false;
private string _threadError = string.Empty;
private Thread _thread;
// 保留参数
private int _timeout;
private int _failedTryAgain;
private int _requestCount;
// 下载结果
private string _downloadError = string.Empty;
private float _downloadProgress = 0f;
private long _downloadBytes = 0;
/// <summary>
/// 下载进度(0-100f)
/// </summary>
public float DownloadProgress
{
get
{
return _downloadProgress;
}
}
/// <summary>
/// 已经下载的总字节数
/// </summary>
public long DownloadedBytes
{
get
{
return _downloadBytes;
}
}
internal void SendRequest(int failedTryAgain, int timeout)
{
}
internal void Update()
{
if (_steps == ESteps.None)
return;
if (_steps == ESteps.Succeed || _steps == ESteps.Failed)
return;
if(_steps == ESteps.CreateDownload)
{
_downloadError = string.Empty;
_downloadProgress = 0f;
_downloadBytes = 0;
_threadOver = false;
_threadResult = false;
_threadError = string.Empty;
_thread = new Thread(ThreadRun);
_thread.IsBackground = true;
_thread.Start();
_steps = ESteps.CheckDownload;
}
if(_steps == ESteps.CheckDownload)
{
if (_threadOver == false)
return;
if(_thread != null)
{
_thread.Abort();
_thread = null;
}
_downloadError = _threadError;
if (_threadResult)
{
DownloadSystem.CacheVerifyFile(BundleInfo.Hash, BundleInfo.BundleName);
_steps = ESteps.Succeed;
}
else
{
// 失败后重新尝试
if(_failedTryAgain > 0)
{
_failedTryAgain--;
_steps = ESteps.CreateDownload;
}
else
{
_steps = ESteps.Failed;
}
}
}
}
internal void SetDone()
{
_steps = ESteps.Succeed;
}
/// <summary>
/// 检测下载器是否已经完成(无论成功或失败)
/// </summary>
public bool IsDone()
{
return _steps == ESteps.Succeed || _steps == ESteps.Failed;
}
/// <summary>
/// 下载过程是否发生错误
/// </summary>
public bool HasError()
{
return _steps == ESteps.Failed;
}
/// <summary>
/// 报告错误信息
/// </summary>
public void ReportError()
{
Logger.Error(_downloadError);
}
#region 多线程下载
public const int BufferSize = 1042 * 4;
private void ThreadRun()
{
string url = GetRequestURL();
string savePath = BundleInfo.LocalPath;
long fileTotalSize = BundleInfo.SizeBytes;
FileStream fileStream = null;
Stream webStream = null;
HttpWebResponse fileResponse = null;
try
{
// 创建文件流
fileStream = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
long fileLength = fileStream.Length;
// 创建HTTP下载请求
HttpWebRequest fileRequest = WebRequest.Create(url) as HttpWebRequest;
fileRequest.Timeout = _timeout;
fileRequest.ReadWriteTimeout = _timeout;
fileRequest.ProtocolVersion = HttpVersion.Version10;
if (fileLength > 0)
{
// 注意:设置远端请求文件的起始位置
fileRequest.AddRange(fileLength);
// 注意:设置本地文件流的起始位置
fileStream.Seek(fileLength, SeekOrigin.Begin);
}
// 读取下载数据并保存到文件
fileResponse = fileRequest.GetResponse() as HttpWebResponse;
webStream = fileResponse.GetResponseStream();
byte[] buffer = new byte[BufferSize];
while (true)
{
int length = webStream.Read(buffer, 0, buffer.Length);
if (length <= 0)
break;
fileStream.Write(buffer, 0, length);
// 计算下载进度
// 注意:原子操作保证数据安全
fileLength += length;
float progress = (fileLength / fileTotalSize) * 100f;
_downloadProgress = progress;
_downloadBytes = fileLength;
}
// 验证下载文件完整性
bool verfiyResult = DownloadSystem.CheckContentIntegrity(savePath, BundleInfo.SizeBytes, BundleInfo.CRC);
if(verfiyResult)
{
_threadResult = true;
}
else
{
_threadResult = false;
_threadError = $"Verify file content failed : {BundleInfo.Hash}";
}
}
catch (Exception e)
{
_threadResult = false;
_threadError = e.Message;
}
finally
{
if (webStream != null)
{
webStream.Close();
webStream.Dispose();
}
if (fileResponse != null)
{
fileResponse.Close();
}
if (fileStream != null)
{
fileStream.Close();
fileStream.Dispose();
}
_threadOver = true;
}
}
private string GetRequestURL()
{
// 轮流返回请求地址
_requestCount++;
if (_requestCount % 2 == 0)
return BundleInfo.RemoteFallbackURL;
else
return BundleInfo.RemoteMainURL;
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 057a358d2d0c92b47add59dac8bef783
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,175 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 补丁下载器
/// </summary>
public class PatchDownloader : IEnumerator
{
private const int MAX_LOADER_COUNT = 64;
public delegate void OnDownloadOver(bool isSucceed);
public delegate void OnDownloadProgress(int totalDownloadCount, int currentDownloadCoun, long totalDownloadBytes, long currentDownloadBytes);
public delegate void OnPatchFileDownloadFailed(string fileName);
private readonly HostPlayModeImpl _playModeImpl;
private readonly int _fileLoadingMaxNumber;
private readonly int _failedTryAgain;
private readonly List<AssetBundleInfo> _downloadList;
private readonly List<AssetBundleInfo> _loadFailedList = new List<AssetBundleInfo>();
private readonly List<FileDownloader> _downloaders = new List<FileDownloader>();
private readonly List<FileDownloader> _removeList = new List<FileDownloader>(MAX_LOADER_COUNT);
// 数据相关
public EDownloaderStates DownloadStates { private set; get; }
public int TotalDownloadCount { private set; get; }
public long TotalDownloadBytes { private set; get; }
public int CurrentDownloadCount { private set; get; }
public long CurrentDownloadBytes { private set; get; }
private long _lastDownloadBytes = 0;
private int _lastDownloadCount = 0;
// 委托相关
public OnDownloadOver OnDownloadOverCallback { set; get; }
public OnDownloadProgress OnDownloadProgressCallback { set; get; }
public OnPatchFileDownloadFailed OnPatchFileDownloadFailedCallback { set; get; }
private PatchDownloader()
{
}
internal PatchDownloader(HostPlayModeImpl playModeImpl, List<AssetBundleInfo> downloadList, int fileLoadingMaxNumber, int failedTryAgain)
{
_playModeImpl = playModeImpl;
_downloadList = downloadList;
_fileLoadingMaxNumber = UnityEngine.Mathf.Clamp(fileLoadingMaxNumber, 1, MAX_LOADER_COUNT); ;
_failedTryAgain = failedTryAgain;
DownloadStates = EDownloaderStates.None;
TotalDownloadCount = downloadList.Count;
foreach (var patchBundle in downloadList)
{
TotalDownloadBytes += patchBundle.SizeBytes;
}
}
/// <summary>
/// 是否完毕,无论成功或失败
/// </summary>
public bool IsDone()
{
return DownloadStates == EDownloaderStates.Failed || DownloadStates == EDownloaderStates.Succeed;
}
/// <summary>
/// 开始下载
/// </summary>
public void Download()
{
if (DownloadStates != EDownloaderStates.None)
{
Logger.Warning($"{nameof(PatchDownloader)} is already running.");
return;
}
Logger.Log($"Begine to download : {TotalDownloadCount} files and {TotalDownloadBytes} bytes");
DownloadStates = EDownloaderStates.Loading;
}
/// <summary>
/// 更新下载器
/// </summary>
public void Update()
{
if (DownloadStates != EDownloaderStates.Loading)
return;
// 检测下载器结果
_removeList.Clear();
long downloadBytes = CurrentDownloadBytes;
foreach (var downloader in _downloaders)
{
downloadBytes += (long)downloader.DownloadedBytes;
if (downloader.IsDone() == false)
continue;
AssetBundleInfo bundleInfo = downloader.BundleInfo;
// 检测是否下载失败
if (downloader.HasError())
{
downloader.ReportError();
_removeList.Add(downloader);
_loadFailedList.Add(bundleInfo);
continue;
}
// 下载成功
_removeList.Add(downloader);
CurrentDownloadCount++;
CurrentDownloadBytes += bundleInfo.SizeBytes;
}
// 移除已经完成的下载器(无论成功或失败)
foreach (var loader in _removeList)
{
_downloaders.Remove(loader);
}
// 如果下载进度发生变化
if (_lastDownloadBytes != downloadBytes || _lastDownloadCount != CurrentDownloadCount)
{
_lastDownloadBytes = downloadBytes;
_lastDownloadCount = CurrentDownloadCount;
OnDownloadProgressCallback?.Invoke(TotalDownloadCount, _lastDownloadCount, TotalDownloadBytes, _lastDownloadBytes);
}
// 动态创建新的下载器到最大数量限制
// 注意:如果期间有下载失败的文件,暂停动态创建下载器
if (_downloadList.Count > 0 && _loadFailedList.Count == 0)
{
if (_downloaders.Count < _fileLoadingMaxNumber)
{
int index = _downloadList.Count - 1;
var operation = DownloadSystem.BeginDownload(_downloadList[index], _failedTryAgain);
_downloaders.Add(operation);
_downloadList.RemoveAt(index);
}
}
// 下载结算
if (_downloaders.Count == 0)
{
if (_loadFailedList.Count > 0)
{
DownloadStates = EDownloaderStates.Failed;
OnPatchFileDownloadFailedCallback?.Invoke(_loadFailedList[0].BundleName);
OnDownloadOverCallback?.Invoke(false);
}
else
{
// 结算成功
DownloadStates = EDownloaderStates.Succeed;
OnDownloadOverCallback?.Invoke(true);
}
}
}
#region 异步相关
bool IEnumerator.MoveNext()
{
return !IsDone();
}
void IEnumerator.Reset()
{
}
object IEnumerator.Current
{
get { return null; }
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e6483a640bdc429459305f148a863397
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,38 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Threading;
namespace YooAsset
{
/// <summary>
/// 同步其它线程里的回调到主线程里
/// 注意:Unity3D中需要设置Scripting Runtime Version为.NET4.6
/// </summary>
internal sealed class ThreadSyncContext : SynchronizationContext
{
private readonly ConcurrentQueue<Action> _safeQueue = new ConcurrentQueue<Action>();
/// <summary>
/// 更新同步队列
/// </summary>
public void Update()
{
while (true)
{
if (_safeQueue.TryDequeue(out Action action) == false)
return;
action.Invoke();
}
}
/// <summary>
/// 向同步队列里投递一个回调方法
/// </summary>
public override void Post(SendOrPostCallback callback, object state)
{
Action action = new Action(() => { callback(state); });
_safeQueue.Enqueue(action);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1c8ce2c52a3e9964fa50a9c031e4e593
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,113 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
namespace YooAsset
{
/// <summary>
/// 下载器
/// 说明:UnityWebRequest(UWR) supports reading streaming assets since 2017.1
/// </summary>
internal class UnityWebRequester
{
protected UnityWebRequest _webRequest;
protected UnityWebRequestAsyncOperation _operationHandle;
/// <summary>
/// 请求URL地址
/// </summary>
public string URL { private set; get; }
/// <summary>
/// 发送GET请求
/// </summary>
/// <param name="timeout">超时:从请求开始计时</param>
public void SendRequest(string url, int timeout = 0)
{
if (_webRequest == null)
{
URL = url;
_webRequest = new UnityWebRequest(URL, UnityWebRequest.kHttpVerbGET);
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
_webRequest.downloadHandler = handler;
_webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.timeout = timeout;
_operationHandle = _webRequest.SendWebRequest();
}
}
/// <summary>
/// 获取下载的字节数据
/// </summary>
public byte[] GetData()
{
if (_webRequest != null && IsDone())
return _webRequest.downloadHandler.data;
else
return null;
}
/// <summary>
/// 获取下载的文本数据
/// </summary>
public string GetText()
{
if (_webRequest != null && IsDone())
return _webRequest.downloadHandler.text;
else
return null;
}
/// <summary>
/// 释放下载器
/// </summary>
public void Dispose()
{
if (_webRequest != null)
{
_webRequest.Dispose();
_webRequest = null;
_operationHandle = null;
}
}
/// <summary>
/// 是否完毕(无论成功失败)
/// </summary>
public bool IsDone()
{
if (_operationHandle == null)
return false;
return _operationHandle.isDone;
}
/// <summary>
/// 下载是否发生错误
/// </summary>
public bool HasError()
{
#if UNITY_2020_3_OR_NEWER
return _webRequest.result != UnityWebRequest.Result.Success;
#else
if (_webRequest.isNetworkError || _webRequest.isHttpError)
return true;
else
return false;
#endif
}
/// <summary>
/// 获取错误信息
/// </summary>
public string GetError()
{
if (_webRequest != null)
{
return $"URL : {URL} Error : {_webRequest.error}";
}
return string.Empty;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b7557eb146572de49a1ec9b3f3c0b706
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: