refactor : 重构异步操作模块

This commit is contained in:
何冠峰
2026-01-12 16:10:24 +08:00
parent 294fa18fec
commit 7198e639d9
4 changed files with 57 additions and 20 deletions

View File

@@ -155,9 +155,19 @@ namespace YooAsset
if (_childs == null)
_childs = new List<AsyncOperationBase>(10);
#if UNITY_EDITOR
#if UNITY_EDITOR || DEBUG
if (child == null)
throw new YooInternalException("The child node is null !");
if (ReferenceEquals(child, this))
throw new YooInternalException("The child node cannot be itself !");
if (_childs.Contains(child))
throw new YooInternalException($"The child node {child.GetType().Name} already exists !");
// 禁止形成环依赖
if (WouldCreateCycle(child))
throw new YooInternalException($"AddChildOperation would create a cycle : {this.GetType().Name} -> {child.GetType().Name}");
#endif
_childs.Add(child);
@@ -171,7 +181,10 @@ namespace YooAsset
if (_childs == null)
return;
#if UNITY_EDITOR
#if UNITY_EDITOR || DEBUG
if (child == null)
throw new YooInternalException("The child node is null !");
if (_childs.Contains(child) == false)
throw new YooInternalException($"The child node {child.GetType().Name} not exists !");
#endif
@@ -349,10 +362,10 @@ namespace YooAsset
if (IsWaitForAsyncComplete == false)
{
IsWaitForAsyncComplete = true;
if (IsDone == false)
InternalWaitForAsyncComplete();
if (IsDone == false)
{
Status = EOperationStatus.Failed;
@@ -415,6 +428,39 @@ namespace YooAsset
float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f);
return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
}
private bool WouldCreateCycle(AsyncOperationBase child)
{
const int maxVisited = 4096;
var stack = new Stack<AsyncOperationBase>();
var visited = new HashSet<AsyncOperationBase>();
stack.Push(child);
while (stack.Count > 0)
{
var node = stack.Pop();
if (node == null)
continue;
if (visited.Add(node) == false)
continue;
if (visited.Count > maxVisited)
throw new YooInternalException("Child operation graph is too large, cycle check aborted !");
if (ReferenceEquals(node, this))
return true;
if (node._childs == null)
continue;
for (int i = 0; i < node._childs.Count; i++)
{
stack.Push(node._childs[i]);
}
}
return false;
}
internal DebugOperationInfo GetDebugOperationInfo()
{

View File

@@ -97,6 +97,8 @@ namespace YooAsset
/// </summary>
public static void DestroyAll()
{
YooLogger.Log("Operation system destroy all !");
// 清空所有调度器
foreach (var scheduler in _schedulerList)
{
@@ -177,7 +179,7 @@ namespace YooAsset
}
// 严格模式:非默认包裹必须先创建调度器
throw new YooInternalException($"Package scheduler not found: {packageName}. Please call YooAssets.CreatePackage() first!");
throw new YooInternalException($"Operation scheduler not found: {packageName}. Please call YooAssets.CreatePackage() first!");
}
#region

View File

@@ -59,6 +59,7 @@ namespace YooAsset
_driver.AddComponent<RemoteDebuggerInRuntime>();
#endif
// 初始化异步操作系统
OperationSystem.Initialize();
}
}
@@ -75,8 +76,8 @@ namespace YooAsset
if (_driver != null)
GameObject.Destroy(_driver);
// 终止并清空所有包裹的异步操作
ClearAllPackageOperation();
// 销毁异步操作系统
OperationSystem.DestroyAll();
// 卸载所有AssetBundle
AssetBundle.UnloadAllAssetBundles(true);
@@ -97,18 +98,6 @@ namespace YooAsset
}
}
/// <summary>
/// 终止并清空所有包裹的异步操作
/// </summary>
internal static void ClearAllPackageOperation()
{
foreach (var package in _packages)
{
OperationSystem.ClearPackageOperation(package.PackageName);
}
OperationSystem.DestroyAll();
}
/// <summary>
/// 创建资源包裹
/// </summary>

View File

@@ -25,7 +25,7 @@ namespace YooAsset
void OnApplicationQuit()
{
// 说明在编辑器下确保播放被停止时IO类操作被终止。
YooAssets.ClearAllPackageOperation();
YooAssets.Destroy();
}
#endif