refactor : 重构异步操作模块

This commit is contained in:
何冠峰
2026-01-13 17:01:15 +08:00
parent 354ca5197f
commit 246a62a675
12 changed files with 67 additions and 84 deletions
@@ -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);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fd52bc7cb896369498d42a12081816ee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -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)