2025-01-09 11:31:04 +08:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace YooAsset
|
|
|
|
|
{
|
|
|
|
|
internal abstract class UnityWebRequestOperation : AsyncOperationBase
|
|
|
|
|
{
|
|
|
|
|
protected UnityWebRequest _webRequest;
|
|
|
|
|
protected readonly string _requestURL;
|
|
|
|
|
private bool _isAbort = false;
|
|
|
|
|
|
2025-09-02 19:21:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// HTTP返回码
|
|
|
|
|
/// </summary>
|
|
|
|
|
public long HttpCode { private set; get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前下载的字节数
|
|
|
|
|
/// </summary>
|
|
|
|
|
public long DownloadedBytes { protected set; get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前下载进度(0f - 1f)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float DownloadProgress { protected set; get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 请求的URL地址
|
|
|
|
|
/// </summary>
|
2025-01-09 11:31:04 +08:00
|
|
|
|
public string URL
|
|
|
|
|
{
|
|
|
|
|
get { return _requestURL; }
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 19:21:49 +08:00
|
|
|
|
internal UnityWebRequestOperation(string url)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
_requestURL = url;
|
2025-09-02 19:21:49 +08:00
|
|
|
|
}
|
|
|
|
|
internal override void InternalAbort()
|
|
|
|
|
{
|
|
|
|
|
//TODO
|
|
|
|
|
// 1. 编辑器下停止运行游戏的时候主动终止下载任务
|
|
|
|
|
// 2. 真机上销毁包裹的时候主动终止下载任务
|
|
|
|
|
if (_isAbort == false)
|
|
|
|
|
{
|
|
|
|
|
if (_webRequest != null)
|
|
|
|
|
{
|
|
|
|
|
_webRequest.Abort();
|
|
|
|
|
_isAbort = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 释放下载器
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected void DisposeRequest()
|
|
|
|
|
{
|
|
|
|
|
if (_webRequest != null)
|
|
|
|
|
{
|
2025-09-02 19:21:49 +08:00
|
|
|
|
//注意:引擎底层会自动调用Abort方法
|
2025-01-09 11:31:04 +08:00
|
|
|
|
_webRequest.Dispose();
|
|
|
|
|
_webRequest = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 检测请求结果
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected bool CheckRequestResult()
|
|
|
|
|
{
|
2025-09-02 19:21:49 +08:00
|
|
|
|
HttpCode = _webRequest.responseCode;
|
|
|
|
|
|
2025-01-09 11:31:04 +08:00
|
|
|
|
#if UNITY_2020_3_OR_NEWER
|
|
|
|
|
if (_webRequest.result != UnityWebRequest.Result.Success)
|
|
|
|
|
{
|
|
|
|
|
Error = $"URL : {_requestURL} Error : {_webRequest.error}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
|
|
|
|
{
|
|
|
|
|
Error = $"URL : {_requestURL} Error : {_webRequest.error}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|