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