【m】框架大更新
This commit is contained in:
118
Assets/03.YooAsset/Runtime/DownloadSystem/DownloadDefine.cs
Normal file
118
Assets/03.YooAsset/Runtime/DownloadSystem/DownloadDefine.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载器结束
|
||||
/// </summary>
|
||||
public struct DownloaderFinishData
|
||||
{
|
||||
/// <summary>
|
||||
/// 所属包裹名称
|
||||
/// </summary>
|
||||
public string PackageName;
|
||||
|
||||
/// <summary>
|
||||
/// 是否成功
|
||||
/// </summary>
|
||||
public bool Succeed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载器相关的更新数据
|
||||
/// </summary>
|
||||
public struct DownloadUpdateData
|
||||
{
|
||||
/// <summary>
|
||||
/// 所属包裹名称
|
||||
/// </summary>
|
||||
public string PackageName;
|
||||
|
||||
/// <summary>
|
||||
/// 下载进度 (0-1f)
|
||||
/// </summary>
|
||||
public float Progress;
|
||||
|
||||
/// <summary>
|
||||
/// 下载文件总数
|
||||
/// </summary>
|
||||
public int TotalDownloadCount;
|
||||
|
||||
/// <summary>
|
||||
/// 当前完成的下载文件数量
|
||||
/// </summary>
|
||||
public int CurrentDownloadCount;
|
||||
|
||||
/// <summary>
|
||||
/// 下载数据总大小(单位:字节)
|
||||
/// </summary>
|
||||
public long TotalDownloadBytes;
|
||||
|
||||
/// <summary>
|
||||
/// 当前完成的下载数据大小(单位:字节)
|
||||
/// </summary>
|
||||
public long CurrentDownloadBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载器相关的错误数据
|
||||
/// </summary>
|
||||
public struct DownloadErrorData
|
||||
{
|
||||
/// <summary>
|
||||
/// 所属包裹名称
|
||||
/// </summary>
|
||||
public string PackageName;
|
||||
|
||||
/// <summary>
|
||||
/// 下载失败的文件名称
|
||||
/// </summary>
|
||||
public string FileName;
|
||||
|
||||
/// <summary>
|
||||
/// 错误信息
|
||||
/// </summary>
|
||||
public string ErrorInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载器相关的文件数据
|
||||
/// </summary>
|
||||
public struct DownloadFileData
|
||||
{
|
||||
/// <summary>
|
||||
/// 所属包裹名称
|
||||
/// </summary>
|
||||
public string PackageName;
|
||||
|
||||
/// <summary>
|
||||
/// 下载的文件名称
|
||||
/// </summary>
|
||||
public string FileName;
|
||||
|
||||
/// <summary>
|
||||
/// 下载的文件大小
|
||||
/// </summary>
|
||||
public long FileSize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导入文件的信息
|
||||
/// </summary>
|
||||
public struct ImportFileInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 本地文件路径
|
||||
/// </summary>
|
||||
public string FilePath;
|
||||
|
||||
/// <summary>
|
||||
/// 资源包名称
|
||||
/// </summary>
|
||||
public string BundleName;
|
||||
|
||||
/// <summary>
|
||||
/// 资源包GUID
|
||||
/// </summary>
|
||||
public string BundleGUID;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6855c00f53b44f44b8eb483934b8abe9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,105 @@
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 自定义下载器的请求委托
|
||||
/// </summary>
|
||||
public delegate UnityWebRequest UnityWebRequestDelegate(string url);
|
||||
|
||||
internal class DownloadSystemHelper
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void OnRuntimeInitialize()
|
||||
{
|
||||
UnityWebRequestCreater = null;
|
||||
}
|
||||
#endif
|
||||
|
||||
public static UnityWebRequestDelegate UnityWebRequestCreater = null;
|
||||
public static UnityWebRequest NewUnityWebRequestGet(string requestURL)
|
||||
{
|
||||
UnityWebRequest webRequest;
|
||||
if (UnityWebRequestCreater != null)
|
||||
webRequest = UnityWebRequestCreater.Invoke(requestURL);
|
||||
else
|
||||
webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET);
|
||||
return webRequest;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取WWW加载本地资源的路径
|
||||
/// </summary>
|
||||
public static string ConvertToWWWPath(string path)
|
||||
{
|
||||
string url;
|
||||
|
||||
// 获取对应平台的URL地址
|
||||
// 说明:苹果不同设备上操作系统不同。
|
||||
// 说明:iPhone和iPod对应的是iOS系统。
|
||||
// 说明:iPad对应的是iPadOS系统。
|
||||
// 说明:AppleTV对应的是tvOS系统。
|
||||
#if UNITY_EDITOR_OSX
|
||||
url = StringUtility.Format("file://{0}", path);
|
||||
#elif UNITY_EDITOR_WIN
|
||||
url = StringUtility.Format("file:///{0}", path);
|
||||
#elif UNITY_WEBGL
|
||||
url = path;
|
||||
#elif UNITY_IOS || UNITY_IPHONE
|
||||
url = StringUtility.Format("file://{0}", path);
|
||||
#elif UNITY_ANDROID
|
||||
if (path.StartsWith("jar:file://"))
|
||||
url = path;
|
||||
else
|
||||
url = StringUtility.Format("jar:file://{0}", path);
|
||||
#elif UNITY_OPENHARMONY
|
||||
if (UnityEngine.Application.streamingAssetsPath.StartsWith("jar:file://"))
|
||||
{
|
||||
if (path.StartsWith("jar:file://"))
|
||||
url = path;
|
||||
else
|
||||
url = StringUtility.Format("jar:file://{0}", path);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (path.StartsWith("file://"))
|
||||
url = path;
|
||||
else
|
||||
url = StringUtility.Format("file://{0}", path);
|
||||
}
|
||||
|
||||
#elif UNITY_WSA
|
||||
url = StringUtility.Format("file:///{0}", path);
|
||||
#elif UNITY_TVOS
|
||||
url = StringUtility.Format("file:///{0}", path);
|
||||
#elif UNITY_STANDALONE_OSX
|
||||
url = new System.Uri(path).ToString();
|
||||
#elif UNITY_STANDALONE_WIN
|
||||
url = StringUtility.Format("file:///{0}", path);
|
||||
#elif UNITY_STANDALONE_LINUX
|
||||
url = StringUtility.Format("file:///root/{0}", path);
|
||||
#else
|
||||
throw new System.NotImplementedException();
|
||||
#endif
|
||||
|
||||
// For some special cases when users have special characters in their devices, url paths can not be identified correctly.
|
||||
return url.Replace("+", "%2B").Replace("#", "%23").Replace("?", "%3F");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否请求的本地文件
|
||||
/// </summary>
|
||||
public static bool IsRequestLocalFile(string url)
|
||||
{
|
||||
//TODO UNITY_STANDALONE_OSX平台目前无法确定
|
||||
if (url.StartsWith("file:"))
|
||||
return true;
|
||||
if (url.StartsWith("jar:file:"))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 32159e0c28a3d4f4ba2e91f9e1712dc4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/03.YooAsset/Runtime/DownloadSystem/Operation.meta
Normal file
8
Assets/03.YooAsset/Runtime/DownloadSystem/Operation.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2a5f52a504c7434fb7965cedf98cd5e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 579b76676616c92489ef30114d81dd1e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,113 @@
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class UnityAssetBundleRequestOperation : UnityWebRequestOperation
|
||||
{
|
||||
protected enum ESteps
|
||||
{
|
||||
None,
|
||||
CreateRequest,
|
||||
Download,
|
||||
Done,
|
||||
}
|
||||
|
||||
private UnityWebRequestAsyncOperation _requestOperation;
|
||||
private DownloadHandlerAssetBundle _downloadhandler;
|
||||
private readonly PackageBundle _packageBundle;
|
||||
private readonly bool _disableUnityWebCache;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
/// <summary>
|
||||
/// 请求结果
|
||||
/// </summary>
|
||||
public AssetBundle Result { private set; get; }
|
||||
|
||||
internal UnityAssetBundleRequestOperation(PackageBundle packageBundle, bool disableUnityWebCache, string url) : base(url)
|
||||
{
|
||||
_packageBundle = packageBundle;
|
||||
_disableUnityWebCache = disableUnityWebCache;
|
||||
}
|
||||
internal override void InternalStart()
|
||||
{
|
||||
_steps = ESteps.CreateRequest;
|
||||
}
|
||||
internal override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.CreateRequest)
|
||||
{
|
||||
CreateWebRequest();
|
||||
_steps = ESteps.Download;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
DownloadProgress = _webRequest.downloadProgress;
|
||||
DownloadedBytes = (long)_webRequest.downloadedBytes;
|
||||
Progress = _requestOperation.progress;
|
||||
if (_requestOperation.isDone == false)
|
||||
return;
|
||||
|
||||
if (CheckRequestResult())
|
||||
{
|
||||
AssetBundle assetBundle = _downloadhandler.assetBundle;
|
||||
if (assetBundle == null)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"URL : {_requestURL} Download handler asset bundle object is null !";
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Result = assetBundle;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
|
||||
// 注意:最终释放请求器
|
||||
DisposeRequest();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateWebRequest()
|
||||
{
|
||||
_downloadhandler = CreateWebDownloadHandler();
|
||||
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
|
||||
_webRequest.downloadHandler = _downloadhandler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_requestOperation = _webRequest.SendWebRequest();
|
||||
}
|
||||
private DownloadHandlerAssetBundle CreateWebDownloadHandler()
|
||||
{
|
||||
if (_disableUnityWebCache)
|
||||
{
|
||||
var downloadhandler = new DownloadHandlerAssetBundle(_requestURL, _packageBundle.UnityCRC);
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
downloadhandler.autoLoadAssetBundle = false;
|
||||
#endif
|
||||
return downloadhandler;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 注意:优先从浏览器缓存里获取文件
|
||||
// The file hash defining the version of the asset bundle.
|
||||
Hash128 fileHash = Hash128.Parse(_packageBundle.FileHash);
|
||||
var downloadhandler = new DownloadHandlerAssetBundle(_requestURL, fileHash, _packageBundle.UnityCRC);
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
downloadhandler.autoLoadAssetBundle = false;
|
||||
#endif
|
||||
return downloadhandler;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8273a13a03f0b9f42a15418990442c34
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,65 @@
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class UnityVirtualBundleRequestOperation : UnityWebRequestOperation
|
||||
{
|
||||
protected enum ESteps
|
||||
{
|
||||
None,
|
||||
Download,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly PackageBundle _bundle;
|
||||
private readonly int _downloadSpeed;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
internal UnityVirtualBundleRequestOperation(PackageBundle packageBundle, int downloadSpeed, string url) : base(url)
|
||||
{
|
||||
_bundle = packageBundle;
|
||||
_downloadSpeed = downloadSpeed;
|
||||
}
|
||||
internal override void InternalStart()
|
||||
{
|
||||
_steps = ESteps.Download;
|
||||
}
|
||||
internal override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
// 模拟下载进度
|
||||
float progress = 0;
|
||||
if (DownloadedBytes > 0)
|
||||
progress = DownloadedBytes / _bundle.FileSize;
|
||||
long downloadBytes = (long)((double)_downloadSpeed * Time.deltaTime);
|
||||
|
||||
Progress = progress;
|
||||
DownloadProgress = progress;
|
||||
DownloadedBytes += downloadBytes;
|
||||
if (DownloadedBytes < _bundle.FileSize)
|
||||
return;
|
||||
|
||||
Progress = 1f;
|
||||
DownloadProgress = 1f;
|
||||
DownloadedBytes = _bundle.FileSize;
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
internal override void InternalWaitForAsyncComplete()
|
||||
{
|
||||
if (_steps != ESteps.Done)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"Try load bundle {_bundle.BundleName} from remote !";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02380d1d22e4169478dfef961e218488
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,89 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class UnityWebCacheRequestOperation : UnityWebRequestOperation
|
||||
{
|
||||
protected enum ESteps
|
||||
{
|
||||
None,
|
||||
CreateRequest,
|
||||
Download,
|
||||
Done,
|
||||
}
|
||||
|
||||
private UnityWebRequestAsyncOperation _requestOperation;
|
||||
private readonly Dictionary<string, string> _headers = new Dictionary<string, string>();
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
|
||||
internal UnityWebCacheRequestOperation(string url) : base(url)
|
||||
{
|
||||
}
|
||||
internal override void InternalStart()
|
||||
{
|
||||
_steps = ESteps.CreateRequest;
|
||||
}
|
||||
internal override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.CreateRequest)
|
||||
{
|
||||
CreateWebRequest();
|
||||
_steps = ESteps.Download;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
DownloadProgress = _webRequest.downloadProgress;
|
||||
DownloadedBytes = (long)_webRequest.downloadedBytes;
|
||||
Progress = _requestOperation.progress;
|
||||
if (_requestOperation.isDone == false)
|
||||
return;
|
||||
|
||||
if (CheckRequestResult())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
|
||||
// 注意:最终释放请求器
|
||||
DisposeRequest();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置请求头信息
|
||||
/// </summary>
|
||||
public void SetRequestHeader(string name, string value)
|
||||
{
|
||||
_headers.Add(name, value);
|
||||
}
|
||||
|
||||
private void CreateWebRequest()
|
||||
{
|
||||
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
|
||||
// 设置消息头
|
||||
foreach (var keyValuePair in _headers)
|
||||
{
|
||||
string name = keyValuePair.Key;
|
||||
string value = keyValuePair.Value;
|
||||
_webRequest.SetRequestHeader(name, value);
|
||||
}
|
||||
|
||||
_requestOperation = _webRequest.SendWebRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f50f5a2d30eb68f48a1873cbb0148313
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,96 @@
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class UnityWebDataRequestOperation : UnityWebRequestOperation
|
||||
{
|
||||
protected enum ESteps
|
||||
{
|
||||
None,
|
||||
CreateRequest,
|
||||
Download,
|
||||
Done,
|
||||
}
|
||||
|
||||
private UnityWebRequestAsyncOperation _requestOperation;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
/// <summary>
|
||||
/// 响应的超时时间(单位:秒),在经过Timeout的秒数后尝试中止。
|
||||
/// 注意:当Timeout设置为0时,不会应用超时。
|
||||
/// 注意:设置的超时值可能应用于Android上的每个URL重定向,这可能会导致响应时间增加。
|
||||
/// </summary>
|
||||
private readonly int _timeout;
|
||||
|
||||
/// <summary>
|
||||
/// 请求结果
|
||||
/// </summary>
|
||||
public byte[] Result { private set; get; }
|
||||
|
||||
|
||||
internal UnityWebDataRequestOperation(string url, int timeout) : base(url)
|
||||
{
|
||||
_timeout = timeout;
|
||||
}
|
||||
internal override void InternalStart()
|
||||
{
|
||||
_steps = ESteps.CreateRequest;
|
||||
}
|
||||
internal override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.CreateRequest)
|
||||
{
|
||||
CreateWebRequest();
|
||||
_steps = ESteps.Download;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
DownloadProgress = _webRequest.downloadProgress;
|
||||
DownloadedBytes = (long)_webRequest.downloadedBytes;
|
||||
Progress = _requestOperation.progress;
|
||||
if (_requestOperation.isDone == false)
|
||||
return;
|
||||
|
||||
if (CheckRequestResult())
|
||||
{
|
||||
var fileData = _webRequest.downloadHandler.data;
|
||||
if (fileData == null || fileData.Length == 0)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"URL : {_requestURL} Download handler data is null or empty !";
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Result = fileData;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
|
||||
// 注意:最终释放请求器
|
||||
DisposeRequest();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateWebRequest()
|
||||
{
|
||||
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
|
||||
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
|
||||
_webRequest.timeout = _timeout;
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_requestOperation = _webRequest.SendWebRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5aa40001d0f482d45bc4aa0452fd966c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,83 @@
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class UnityWebFileRequestOperation : UnityWebRequestOperation
|
||||
{
|
||||
protected enum ESteps
|
||||
{
|
||||
None,
|
||||
CreateRequest,
|
||||
Download,
|
||||
Done,
|
||||
}
|
||||
|
||||
private UnityWebRequestAsyncOperation _requestOperation;
|
||||
private readonly string _fileSavePath;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
/// <summary>
|
||||
/// 响应的超时时间(单位:秒),在经过Timeout的秒数后尝试中止。
|
||||
/// 注意:当Timeout设置为0时,不会应用超时。
|
||||
/// 注意:设置的超时值可能应用于Android上的每个URL重定向,这可能会导致响应时间增加。
|
||||
/// </summary>
|
||||
private readonly int _timeout;
|
||||
|
||||
|
||||
internal UnityWebFileRequestOperation(string url, string fileSavePath, int timeout) : base(url)
|
||||
{
|
||||
_fileSavePath = fileSavePath;
|
||||
_timeout = timeout;
|
||||
}
|
||||
internal override void InternalStart()
|
||||
{
|
||||
_steps = ESteps.CreateRequest;
|
||||
}
|
||||
internal override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.CreateRequest)
|
||||
{
|
||||
CreateWebRequest();
|
||||
_steps = ESteps.Download;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
DownloadProgress = _webRequest.downloadProgress;
|
||||
DownloadedBytes = (long)_webRequest.downloadedBytes;
|
||||
Progress = _requestOperation.progress;
|
||||
if (_requestOperation.isDone == false)
|
||||
return;
|
||||
|
||||
if (CheckRequestResult())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
|
||||
// 注意:最终释放请求器
|
||||
DisposeRequest();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateWebRequest()
|
||||
{
|
||||
DownloadHandlerFile handler = new DownloadHandlerFile(_fileSavePath);
|
||||
handler.removeFileOnAbort = true;
|
||||
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
|
||||
_webRequest.timeout = _timeout;
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_requestOperation = _webRequest.SendWebRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c78a021ff3b8fa46b987def1b181029
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,98 @@
|
||||
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;
|
||||
|
||||
/// <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>
|
||||
public string URL
|
||||
{
|
||||
get { return _requestURL; }
|
||||
}
|
||||
|
||||
internal UnityWebRequestOperation(string url)
|
||||
{
|
||||
_requestURL = url;
|
||||
}
|
||||
internal override void InternalAbort()
|
||||
{
|
||||
//TODO
|
||||
// 1. 编辑器下停止运行游戏的时候主动终止下载任务
|
||||
// 2. 真机上销毁包裹的时候主动终止下载任务
|
||||
if (_isAbort == false)
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
_webRequest.Abort();
|
||||
_isAbort = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放下载器
|
||||
/// </summary>
|
||||
protected void DisposeRequest()
|
||||
{
|
||||
if (_webRequest != null)
|
||||
{
|
||||
//注意:引擎底层会自动调用Abort方法
|
||||
_webRequest.Dispose();
|
||||
_webRequest = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测请求结果
|
||||
/// </summary>
|
||||
protected bool CheckRequestResult()
|
||||
{
|
||||
HttpCode = _webRequest.responseCode;
|
||||
|
||||
#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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a32e3be14dc5de40b14d9cfb03b0dbf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,96 @@
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class UnityWebTextRequestOperation : UnityWebRequestOperation
|
||||
{
|
||||
protected enum ESteps
|
||||
{
|
||||
None,
|
||||
CreateRequest,
|
||||
Download,
|
||||
Done,
|
||||
}
|
||||
|
||||
private UnityWebRequestAsyncOperation _requestOperation;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
/// <summary>
|
||||
/// 响应的超时时间(单位:秒),在经过Timeout的秒数后尝试中止。
|
||||
/// 注意:当Timeout设置为0时,不会应用超时。
|
||||
/// 注意:设置的超时值可能应用于Android上的每个URL重定向,这可能会导致响应时间增加。
|
||||
/// </summary>
|
||||
private readonly int _timeout;
|
||||
|
||||
/// <summary>
|
||||
/// 请求结果
|
||||
/// </summary>
|
||||
public string Result { private set; get; }
|
||||
|
||||
|
||||
internal UnityWebTextRequestOperation(string url, int timeout) : base(url)
|
||||
{
|
||||
_timeout = timeout;
|
||||
}
|
||||
internal override void InternalStart()
|
||||
{
|
||||
_steps = ESteps.CreateRequest;
|
||||
}
|
||||
internal override void InternalUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.CreateRequest)
|
||||
{
|
||||
CreateWebRequest();
|
||||
_steps = ESteps.Download;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Download)
|
||||
{
|
||||
DownloadProgress = _webRequest.downloadProgress;
|
||||
DownloadedBytes = (long)_webRequest.downloadedBytes;
|
||||
Progress = _requestOperation.progress;
|
||||
if (_requestOperation.isDone == false)
|
||||
return;
|
||||
|
||||
if (CheckRequestResult())
|
||||
{
|
||||
var fileText = _webRequest.downloadHandler.text;
|
||||
if (string.IsNullOrEmpty(fileText))
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"URL : {_requestURL} Download handler text is null or empty !";
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Result = fileText;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
|
||||
// 注意:最终释放请求器
|
||||
DisposeRequest();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateWebRequest()
|
||||
{
|
||||
DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
|
||||
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
|
||||
_webRequest.timeout = _timeout;
|
||||
_webRequest.downloadHandler = handler;
|
||||
_webRequest.disposeDownloadHandlerOnDispose = true;
|
||||
_requestOperation = _webRequest.SendWebRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6edb8d3d4e731224694f46e47187eb5a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class WebRequestCounter
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void OnRuntimeInitialize()
|
||||
{
|
||||
_requestFailedRecorder.Clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// 记录网络请求失败事件的次数
|
||||
/// </summary>
|
||||
private static readonly Dictionary<string, int> _requestFailedRecorder = new Dictionary<string, int>(1000);
|
||||
|
||||
/// <summary>
|
||||
/// 记录请求失败事件
|
||||
/// </summary>
|
||||
public static void RecordRequestFailed(string packageName, string eventName)
|
||||
{
|
||||
string key = $"{packageName}_{eventName}";
|
||||
if (_requestFailedRecorder.ContainsKey(key) == false)
|
||||
_requestFailedRecorder.Add(key, 0);
|
||||
_requestFailedRecorder[key]++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求失败的次数
|
||||
/// </summary>
|
||||
public static int GetRequestFailedCount(string packageName, string eventName)
|
||||
{
|
||||
string key = $"{packageName}_{eventName}";
|
||||
if (_requestFailedRecorder.ContainsKey(key) == false)
|
||||
_requestFailedRecorder.Add(key, 0);
|
||||
return _requestFailedRecorder[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 912349e84adc29a4ba8be9daf03525c4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user