update runtime code

重构了OperationSystem的机制。从列表模式修改为链模式
This commit is contained in:
何冠峰
2025-02-25 12:18:16 +08:00
parent 400c2ccefe
commit c758aa81ff
72 changed files with 385 additions and 260 deletions
@@ -21,7 +21,7 @@ namespace YooAsset
/// <summary>
/// 是否已经完成
/// </summary>
internal bool IsFinish = false;
internal bool IsFinish { private set; get; } = false;
/// <summary>
/// 优先级
@@ -110,11 +110,32 @@ namespace YooAsset
throw new System.NotImplementedException(this.GetType().Name);
}
/// <summary>
/// 设置包裹名称
/// </summary>
/// <param name="packageName"></param>
internal void SetPackageName(string packageName)
{
_packageName = packageName;
}
internal void SetStart()
/// <summary>
/// 添加子任务
/// </summary>
internal void AddChildOperation(AsyncOperationBase child)
{
#if UNITY_EDITOR
if (_childs.Contains(child))
throw new Exception($"The child node {child.GetType().Name} already exists !");
#endif
_childs.Add(child);
}
/// <summary>
/// 开始异步操作
/// </summary>
internal void StartOperation()
{
if (Status == EOperationStatus.None)
{
@@ -122,21 +143,40 @@ namespace YooAsset
InternalStart();
}
}
internal void SetFinish()
/// <summary>
/// 更新异步操作
/// </summary>
internal void UpdateOperation()
{
IsFinish = true;
if (IsDone == false)
InternalUpdate();
// 进度百分百完成
Progress = 1f;
if (IsDone && IsFinish == false)
{
IsFinish = true;
//注意:如果完成回调内发生异常,会导致Task无限期等待
_callback?.Invoke(this);
// 进度百分百完成
Progress = 1f;
if (_taskCompletionSource != null)
_taskCompletionSource.TrySetResult(null);
//注意:如果完成回调内发生异常,会导致Task无限期等待
_callback?.Invoke(this);
if (_taskCompletionSource != null)
_taskCompletionSource.TrySetResult(null);
}
}
internal void SetAbort()
/// <summary>
/// 终止异步任务
/// </summary>
internal void AbortOperation()
{
foreach (var child in _childs)
{
child.AbortOperation();
}
if (IsDone == false)
{
Status = EOperationStatus.Failed;
@@ -188,20 +228,6 @@ namespace YooAsset
InternalWaitForAsyncComplete();
}
/// <summary>
/// 开启子任务
/// </summary>
internal void StartChildOperation(AsyncOperationBase child)
{
#if UNITY_EDITOR
if (_childs.Contains(child))
throw new Exception($"The child node {child.GetType().Name} already exists !");
#endif
child.SetStart();
_childs.Add(child);
}
#region 调试信息
#endregion
@@ -53,7 +53,7 @@ namespace YooAsset
/// </summary>
protected void Abort()
{
SetAbort();
AbortOperation();
}
}
}
@@ -43,8 +43,6 @@ namespace YooAsset
/// </summary>
public static void Update()
{
_frameTime = _watch.ElapsedMilliseconds;
// 添加新增的异步操作
if (_newList.Count > 0)
{
@@ -67,6 +65,7 @@ namespace YooAsset
}
// 更新进行中的异步操作
_frameTime = _watch.ElapsedMilliseconds;
for (int i = 0; i < _operations.Count; i++)
{
if (IsBusy)
@@ -76,11 +75,7 @@ namespace YooAsset
if (operation.IsFinish)
continue;
if (operation.IsDone == false)
operation.InternalUpdate();
if (operation.IsDone)
operation.SetFinish();
operation.UpdateOperation();
}
// 移除已经完成的异步操作
@@ -114,7 +109,7 @@ namespace YooAsset
{
if (operation.PackageName == packageName)
{
operation.SetAbort();
operation.AbortOperation();
}
}
@@ -123,7 +118,7 @@ namespace YooAsset
{
if (operation.PackageName == packageName)
{
operation.SetAbort();
operation.AbortOperation();
}
}
}
@@ -135,7 +130,7 @@ namespace YooAsset
{
_newList.Add(operation);
operation.SetPackageName(packageName);
operation.SetStart();
operation.StartOperation();
}
}
}