mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-31 05:58:47 +00:00
refactor : 重构异步操作模块
This commit is contained in:
@@ -297,7 +297,7 @@ namespace YooAsset
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//TODO 单个回调异常会阻断后续回调
|
//TODO 单个回调异常会阻断后续订阅者
|
||||||
_callback?.Invoke(this);
|
_callback?.Invoke(this);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -350,6 +350,7 @@ namespace YooAsset
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 无限次数的执行更新逻辑,直到任务完成
|
/// 无限次数的执行更新逻辑,直到任务完成
|
||||||
|
/// 注意:该方法会阻塞主线程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sleepMS">休眠时长</param>
|
/// <param name="sleepMS">休眠时长</param>
|
||||||
protected void RunUntilCompletion(int sleepMS = 1)
|
protected void RunUntilCompletion(int sleepMS = 1)
|
||||||
@@ -482,6 +483,10 @@ namespace YooAsset
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取调试信息
|
||||||
|
/// 注意:递归构建子树存在深度风险
|
||||||
|
/// </summary>
|
||||||
internal DebugOperationInfo GetDebugOperationInfo()
|
internal DebugOperationInfo GetDebugOperationInfo()
|
||||||
{
|
{
|
||||||
var operationInfo = new DebugOperationInfo();
|
var operationInfo = new DebugOperationInfo();
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
|
|
||||||
namespace YooAsset
|
|
||||||
{
|
|
||||||
public abstract class GameAsyncOperation : AsyncOperationBase
|
|
||||||
{
|
|
||||||
internal override void InternalStart()
|
|
||||||
{
|
|
||||||
OnStart();
|
|
||||||
}
|
|
||||||
internal override void InternalUpdate()
|
|
||||||
{
|
|
||||||
OnUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 异步操作开始
|
|
||||||
/// </summary>
|
|
||||||
protected abstract void OnStart();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 异步操作更新
|
|
||||||
/// </summary>
|
|
||||||
protected abstract void OnUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -26,11 +26,26 @@ namespace YooAsset
|
|||||||
// 计时器相关
|
// 计时器相关
|
||||||
private static Stopwatch _watch;
|
private static Stopwatch _watch;
|
||||||
private static long _frameTime;
|
private static long _frameTime;
|
||||||
|
private static long _maxTimeSlice = long.MaxValue;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步操作系统的每帧最大执行预算(毫秒)
|
/// 异步操作系统的每帧最大执行预算(毫秒)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static long MaxTimeSlice { set; get; } = long.MaxValue;
|
public static long MaxTimeSlice
|
||||||
|
{
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value < 10)
|
||||||
|
{
|
||||||
|
_maxTimeSlice = 10;
|
||||||
|
YooLogger.Warning($"MaxTimeSlice minimum value is 10 milliseconds.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_maxTimeSlice = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步操作系统是否繁忙
|
/// 异步操作系统是否繁忙
|
||||||
@@ -42,11 +57,11 @@ namespace YooAsset
|
|||||||
if (_watch == null)
|
if (_watch == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (MaxTimeSlice == long.MaxValue)
|
if (_maxTimeSlice == long.MaxValue)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// 注意 : 单次调用开销约1微秒
|
// 注意 : 单次调用开销约1微秒
|
||||||
return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice;
|
return _watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -207,17 +207,6 @@ namespace YooAsset
|
|||||||
return package != null;
|
return package != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 开启一个异步操作
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="operation">异步操作对象</param>
|
|
||||||
public static void StartOperation(GameAsyncOperation operation)
|
|
||||||
{
|
|
||||||
// 注意:游戏业务逻辑的包裹填写为空
|
|
||||||
OperationSystem.StartOperation(OperationSystem.GLOBAL_SCHEDULER_NAME, operation);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static ResourcePackage GetPackageInternal(string packageName)
|
private static ResourcePackage GetPackageInternal(string packageName)
|
||||||
{
|
{
|
||||||
foreach (var package in _packages)
|
foreach (var package in _packages)
|
||||||
@@ -259,11 +248,6 @@ namespace YooAsset
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static void SetOperationSystemMaxTimeSlice(long milliseconds)
|
public static void SetOperationSystemMaxTimeSlice(long milliseconds)
|
||||||
{
|
{
|
||||||
if (milliseconds < 10)
|
|
||||||
{
|
|
||||||
milliseconds = 10;
|
|
||||||
YooLogger.Warning($"MaxTimeSlice minimum value is 10 milliseconds.");
|
|
||||||
}
|
|
||||||
OperationSystem.MaxTimeSlice = milliseconds;
|
OperationSystem.MaxTimeSlice = milliseconds;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ using YooAsset;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 拷贝内置清单文件到沙盒目录
|
/// 拷贝内置清单文件到沙盒目录
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CopyBuildinManifestOperation : GameAsyncOperation
|
public class CopyBuildinManifestOperation : AsyncOperationBase
|
||||||
{
|
{
|
||||||
private enum ESteps
|
private enum ESteps
|
||||||
{
|
{
|
||||||
@@ -32,11 +32,11 @@ public class CopyBuildinManifestOperation : GameAsyncOperation
|
|||||||
_packageVersion = packageVersion;
|
_packageVersion = packageVersion;
|
||||||
_backend = new UnityWebRequestBackend();
|
_backend = new UnityWebRequestBackend();
|
||||||
}
|
}
|
||||||
protected override void OnStart()
|
internal override void InternalStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.CheckHashFile;
|
_steps = ESteps.CheckHashFile;
|
||||||
}
|
}
|
||||||
protected override void OnUpdate()
|
internal override void InternalUpdate()
|
||||||
{
|
{
|
||||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
return;
|
return;
|
||||||
@@ -121,9 +121,6 @@ public class CopyBuildinManifestOperation : GameAsyncOperation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override void OnAbort()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetBuildinYooRoot()
|
private string GetBuildinYooRoot()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ using YooAsset;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取包体里的内置资源清单版本
|
/// 获取包体里的内置资源清单版本
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GetBuildinPackageVersionOperation : GameAsyncOperation
|
public class GetBuildinPackageVersionOperation : AsyncOperationBase
|
||||||
{
|
{
|
||||||
private enum ESteps
|
private enum ESteps
|
||||||
{
|
{
|
||||||
@@ -31,11 +31,11 @@ public class GetBuildinPackageVersionOperation : GameAsyncOperation
|
|||||||
_packageName = packageName;
|
_packageName = packageName;
|
||||||
_backend = new UnityWebRequestBackend();
|
_backend = new UnityWebRequestBackend();
|
||||||
}
|
}
|
||||||
protected override void OnStart()
|
internal override void InternalStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.GetPackageVersion;
|
_steps = ESteps.GetPackageVersion;
|
||||||
}
|
}
|
||||||
protected override void OnUpdate()
|
internal override void InternalUpdate()
|
||||||
{
|
{
|
||||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
return;
|
return;
|
||||||
@@ -68,9 +68,6 @@ public class GetBuildinPackageVersionOperation : GameAsyncOperation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override void OnAbort()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetBuildinYooRoot()
|
private string GetBuildinYooRoot()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ using YooAsset;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取沙盒目录里缓存文件大小
|
/// 获取沙盒目录里缓存文件大小
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class GetCacheBundleSizeOperation : GameAsyncOperation
|
public class GetCacheBundleSizeOperation : AsyncOperationBase
|
||||||
{
|
{
|
||||||
private enum ESteps
|
private enum ESteps
|
||||||
{
|
{
|
||||||
@@ -29,11 +29,11 @@ public class GetCacheBundleSizeOperation : GameAsyncOperation
|
|||||||
{
|
{
|
||||||
_packageName = packageName;
|
_packageName = packageName;
|
||||||
}
|
}
|
||||||
protected override void OnStart()
|
internal override void InternalStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.GetCacheFiles;
|
_steps = ESteps.GetCacheFiles;
|
||||||
}
|
}
|
||||||
protected override void OnUpdate()
|
internal override void InternalUpdate()
|
||||||
{
|
{
|
||||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
return;
|
return;
|
||||||
@@ -57,9 +57,6 @@ public class GetCacheBundleSizeOperation : GameAsyncOperation
|
|||||||
Status = EOperationStatus.Succeed;
|
Status = EOperationStatus.Succeed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override void OnAbort()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetCacheDirectoryRoot()
|
private string GetCacheDirectoryRoot()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ using System.Collections.Generic;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using YooAsset;
|
using YooAsset;
|
||||||
|
|
||||||
public class LoadAssetsByTagOperation<TObject> : GameAsyncOperation where TObject : UnityEngine.Object
|
public class LoadAssetsByTagOperation<TObject> : AsyncOperationBase where TObject : UnityEngine.Object
|
||||||
{
|
{
|
||||||
private enum ESteps
|
private enum ESteps
|
||||||
{
|
{
|
||||||
@@ -28,11 +28,11 @@ public class LoadAssetsByTagOperation<TObject> : GameAsyncOperation where TObjec
|
|||||||
{
|
{
|
||||||
_tag = tag;
|
_tag = tag;
|
||||||
}
|
}
|
||||||
protected override void OnStart()
|
internal override void InternalStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.LoadAssets;
|
_steps = ESteps.LoadAssets;
|
||||||
}
|
}
|
||||||
protected override void OnUpdate()
|
internal override void InternalUpdate()
|
||||||
{
|
{
|
||||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
return;
|
return;
|
||||||
@@ -77,7 +77,7 @@ public class LoadAssetsByTagOperation<TObject> : GameAsyncOperation where TObjec
|
|||||||
string error = $"资源类型转换失败:{handle.AssetObject.name}";
|
string error = $"资源类型转换失败:{handle.AssetObject.name}";
|
||||||
Debug.LogError($"{error}");
|
Debug.LogError($"{error}");
|
||||||
AssetObjects.Clear();
|
AssetObjects.Clear();
|
||||||
SetFinish(false, error);
|
SetFailed(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,21 +85,23 @@ public class LoadAssetsByTagOperation<TObject> : GameAsyncOperation where TObjec
|
|||||||
{
|
{
|
||||||
Debug.LogError($"{handle.LastError}");
|
Debug.LogError($"{handle.LastError}");
|
||||||
AssetObjects.Clear();
|
AssetObjects.Clear();
|
||||||
SetFinish(false, handle.LastError);
|
SetFailed(handle.LastError);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SetFinish(true);
|
SetSucceed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override void OnAbort()
|
private void SetSucceed()
|
||||||
{
|
{
|
||||||
|
Status = EOperationStatus.Succeed;
|
||||||
|
_steps = ESteps.Done;
|
||||||
}
|
}
|
||||||
private void SetFinish(bool succeed, string error = "")
|
private void SetFailed(string error)
|
||||||
{
|
{
|
||||||
Error = error;
|
Error = error;
|
||||||
Status = succeed ? EOperationStatus.Succeed : EOperationStatus.Failed;
|
Status = EOperationStatus.Failed;
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ public static class YooAssetsExtension
|
|||||||
public static LoadGameObjectOperation LoadGameObjectAsync(this ResourcePackage resourcePackage, string location, Vector3 position, Quaternion rotation, Transform parent, bool destroyGoOnRelease = false)
|
public static LoadGameObjectOperation LoadGameObjectAsync(this ResourcePackage resourcePackage, string location, Vector3 position, Quaternion rotation, Transform parent, bool destroyGoOnRelease = false)
|
||||||
{
|
{
|
||||||
var operation = new LoadGameObjectOperation(location, position, rotation, parent, destroyGoOnRelease);
|
var operation = new LoadGameObjectOperation(location, position, rotation, parent, destroyGoOnRelease);
|
||||||
YooAssets.StartOperation(operation);
|
OperationSystem.StartOperation(OperationSystem.GLOBAL_SCHEDULER_NAME, operation);
|
||||||
return operation;
|
return operation;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LoadGameObjectOperation : GameAsyncOperation
|
public class LoadGameObjectOperation : AsyncOperationBase
|
||||||
{
|
{
|
||||||
private enum ESteps
|
private enum ESteps
|
||||||
{
|
{
|
||||||
@@ -45,11 +45,11 @@ public class LoadGameObjectOperation : GameAsyncOperation
|
|||||||
_parent = parent;
|
_parent = parent;
|
||||||
_destroyGoOnRelease = destroyGoOnRelease;
|
_destroyGoOnRelease = destroyGoOnRelease;
|
||||||
}
|
}
|
||||||
protected override void OnStart()
|
internal override void InternalStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.LoadAsset;
|
_steps = ESteps.LoadAsset;
|
||||||
}
|
}
|
||||||
protected override void OnUpdate()
|
internal override void InternalUpdate()
|
||||||
{
|
{
|
||||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||||
return;
|
return;
|
||||||
@@ -79,9 +79,6 @@ public class LoadGameObjectOperation : GameAsyncOperation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override void OnAbort()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 释放资源句柄
|
/// 释放资源句柄
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using YooAsset;
|
||||||
|
|
||||||
|
public class OperationHelper
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 开始一个业务实现的自定义异步任务
|
||||||
|
/// </summary>
|
||||||
|
public static void StartOperation(AsyncOperationBase operation)
|
||||||
|
{
|
||||||
|
OperationSystem.StartOperation(OperationSystem.GLOBAL_SCHEDULER_NAME, operation);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: ff8a96dd005f55346986f8a98aff8c99
|
guid: fd52bc7cb896369498d42a12081816ee
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
@@ -6,8 +6,6 @@ public static class OperationMonitor
|
|||||||
{
|
{
|
||||||
public static void RegisterOperationCallback()
|
public static void RegisterOperationCallback()
|
||||||
{
|
{
|
||||||
OperationSystem.RegisterStartCallback(OperationStartCallback);
|
|
||||||
OperationSystem.RegisterFinishCallback(OperationFinishCallback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OperationStartCallback(string packageName, AsyncOperationBase operation)
|
private static void OperationStartCallback(string packageName, AsyncOperationBase operation)
|
||||||
|
|||||||
Reference in New Issue
Block a user