From 5aca0bb9f67164f5fac78aadc3ed4dc1df975b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Wed, 10 Sep 2025 16:04:08 +0800 Subject: [PATCH] update --- .../YooAsset/AsyncOperationBaseExtensions.cs | 104 ++++++-------- .../YooAsset/OperationHandleBaseExtensions.cs | 130 +++++++----------- 2 files changed, 93 insertions(+), 141 deletions(-) diff --git a/Runtime/External/YooAsset/AsyncOperationBaseExtensions.cs b/Runtime/External/YooAsset/AsyncOperationBaseExtensions.cs index f55159e..1e36a83 100644 --- a/Runtime/External/YooAsset/AsyncOperationBaseExtensions.cs +++ b/Runtime/External/YooAsset/AsyncOperationBaseExtensions.cs @@ -10,136 +10,114 @@ namespace Cysharp.Threading.Tasks { return ToUniTask(handle).GetAwaiter(); } - - public static UniTask ToUniTask(this AsyncOperationBase handle, - IProgress progress = null, - PlayerLoopTiming timing = PlayerLoopTiming.Update) + public static UniTask ToUniTask(this AsyncOperationBase handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update) { ThrowArgumentNullException(handle, nameof(handle)); - if(handle.IsDone) + if (handle.IsDone) { return UniTask.CompletedTask; } return new UniTask( - AsyncOperationBaserConfiguredSource.Create( - handle, - timing, - progress, - out var token - ), + AsyncOperationBaserConfiguredSource.Create(handle, timing, progress, out var token), token ); } - sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, - IPlayerLoopItem, - ITaskPoolNode + sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { - private static TaskPool pool; + private static TaskPool _pool; + private AsyncOperationBaserConfiguredSource _nextNode; + private readonly Action _continuationAction; + private AsyncOperationBase _handle; + private IProgress _progress; + private bool _completed; + private UniTaskCompletionSourceCore _core; - private AsyncOperationBaserConfiguredSource nextNode; - - public ref AsyncOperationBaserConfiguredSource NextNode => ref nextNode; + public ref AsyncOperationBaserConfiguredSource NextNode => ref _nextNode; static AsyncOperationBaserConfiguredSource() { - TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => pool.Size); + TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => _pool.Size); } - private readonly Action continuationAction; - private AsyncOperationBase handle; - private IProgress progress; - private bool completed; - private UniTaskCompletionSourceCore core; + AsyncOperationBaserConfiguredSource() { _continuationAction = Continuation; } - AsyncOperationBaserConfiguredSource() { continuationAction = Continuation; } - - public static IUniTaskSource Create(AsyncOperationBase handle, - PlayerLoopTiming timing, - IProgress progress, - out short token) + public static IUniTaskSource Create(AsyncOperationBase handle, PlayerLoopTiming timing, IProgress progress, out short token) { - if(!pool.TryPop(out var result)) + if (!_pool.TryPop(out var result)) { result = new AsyncOperationBaserConfiguredSource(); } - result.handle = handle; - result.progress = progress; - result.completed = false; + result._handle = handle; + result._progress = progress; + result._completed = false; TaskTracker.TrackActiveTask(result, 3); - if(progress != null) + if (progress != null) { PlayerLoopHelper.AddAction(timing, result); } - handle.Completed += result.continuationAction; - - token = result.core.Version; - + handle.Completed += result._continuationAction; + token = result._core.Version; return result; } private void Continuation(AsyncOperationBase _) { - handle.Completed -= continuationAction; + _handle.Completed -= _continuationAction; - if(completed) + if (_completed) { TryReturn(); } else { - completed = true; - if(handle.Status == EOperationStatus.Failed) + _completed = true; + if (_handle.Status == EOperationStatus.Failed) { - core.TrySetException(new Exception(handle.Error)); + _core.TrySetException(new Exception(_handle.Error)); } else { - core.TrySetResult(AsyncUnit.Default); + _core.TrySetResult(AsyncUnit.Default); } } } - - bool TryReturn() + private bool TryReturn() { TaskTracker.RemoveTracking(this); - core.Reset(); - handle = default; - progress = default; - return pool.TryPush(this); + _core.Reset(); + _handle = default; + _progress = default; + return _pool.TryPush(this); } - public UniTaskStatus GetStatus(short token) => core.GetStatus(token); - + public UniTaskStatus GetStatus(short token) => _core.GetStatus(token); public void OnCompleted(Action continuation, object state, short token) { - core.OnCompleted(continuation, state, token); + _core.OnCompleted(continuation, state, token); } - - public void GetResult(short token) { core.GetResult(token); } - - public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus(); - + public void GetResult(short token) { _core.GetResult(token); } + public UniTaskStatus UnsafeGetStatus() => _core.UnsafeGetStatus(); public bool MoveNext() { - if(completed) + if (_completed) { TryReturn(); return false; } - if(!handle.IsDone) + if (!_handle.IsDone) { - progress?.Report(handle.Progress); + _progress?.Report(_handle.Progress); } return true; } } } -} \ No newline at end of file +} diff --git a/Runtime/External/YooAsset/OperationHandleBaseExtensions.cs b/Runtime/External/YooAsset/OperationHandleBaseExtensions.cs index 580d5d7..2e0664a 100644 --- a/Runtime/External/YooAsset/OperationHandleBaseExtensions.cs +++ b/Runtime/External/YooAsset/OperationHandleBaseExtensions.cs @@ -7,77 +7,61 @@ using System.Runtime.CompilerServices; using YooAsset; using static Cysharp.Threading.Tasks.Internal.Error; - namespace Cysharp.Threading.Tasks { - public static class OperationHandleBaseExtensions + public static class HandleBaseExtensions { public static UniTask.Awaiter GetAwaiter(this HandleBase handle) { return ToUniTask(handle).GetAwaiter(); } - - public static UniTask ToUniTask(this HandleBase handle, - IProgress progress = null, - PlayerLoopTiming timing = PlayerLoopTiming.Update) + public static UniTask ToUniTask(this HandleBase handle, IProgress progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update) { ThrowArgumentNullException(handle, nameof(handle)); - if(!handle.IsValid) + if (!handle.IsValid) { return UniTask.CompletedTask; } return new UniTask( - OperationHandleBaserConfiguredSource.Create( - handle, - timing, - progress, - out var token - ), + HandleBaserConfiguredSource.Create(handle, timing, progress, out var token), token ); } - sealed class OperationHandleBaserConfiguredSource : IUniTaskSource, - IPlayerLoopItem, - ITaskPoolNode + sealed class HandleBaserConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { - private static TaskPool pool; + private static TaskPool _pool; + private HandleBaserConfiguredSource _nextNode; + private readonly Action _continuationAction; + private HandleBase _handle; + private IProgress _progress; + private bool _completed; + private UniTaskCompletionSourceCore _core; - private OperationHandleBaserConfiguredSource nextNode; + public ref HandleBaserConfiguredSource NextNode => ref _nextNode; - public ref OperationHandleBaserConfiguredSource NextNode => ref nextNode; - - static OperationHandleBaserConfiguredSource() + static HandleBaserConfiguredSource() { - TaskPool.RegisterSizeGetter(typeof(OperationHandleBaserConfiguredSource), () => pool.Size); + TaskPool.RegisterSizeGetter(typeof(HandleBaserConfiguredSource), () => _pool.Size); } - private readonly Action continuationAction; - private HandleBase handle; - private IProgress progress; - private bool completed; - private UniTaskCompletionSourceCore core; + HandleBaserConfiguredSource() { _continuationAction = Continuation; } - OperationHandleBaserConfiguredSource() { continuationAction = Continuation; } - - public static IUniTaskSource Create(HandleBase handle, - PlayerLoopTiming timing, - IProgress progress, - out short token) + public static IUniTaskSource Create(HandleBase handle, PlayerLoopTiming timing, IProgress progress, out short token) { - if(!pool.TryPop(out var result)) + if (!_pool.TryPop(out var result)) { - result = new OperationHandleBaserConfiguredSource(); + result = new HandleBaserConfiguredSource(); } - result.handle = handle; - result.progress = progress; - result.completed = false; + result._handle = handle; + result._progress = progress; + result._completed = false; TaskTracker.TrackActiveTask(result, 3); - if(progress != null) + if (progress != null) { PlayerLoopHelper.AddAction(timing, result); } @@ -85,9 +69,9 @@ namespace Cysharp.Threading.Tasks // BUG 在 Unity 2020.3.36 版本测试中, IL2Cpp 会报 如下错误 // BUG ArgumentException: Incompatible Delegate Types. First is System.Action`1[[YooAsset.AssetHandle, YooAsset, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]] second is System.Action`1[[YooAsset.OperationHandleBase, YooAsset, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]] // BUG 也可能报的是 Action '1' Action '1' 的 InvalidCastException - // BUG 此处不得不这么修改, 如果后续 Unity 修复了这个问题, 可以恢复之前的写法 + // BUG 此处不得不这么修改, 如果后续 Unity 修复了这个问题, 可以恢复之前的写法 #if UNITY_2020_BUG - switch(handle) + switch (handle) { case AssetHandle asset_handle: asset_handle.Completed += result.AssetContinuation; @@ -125,37 +109,33 @@ namespace Cysharp.Threading.Tasks break; } #endif - token = result.core.Version; - + token = result._core.Version; return result; } + #if UNITY_2020_BUG private void AssetContinuation(AssetHandle handle) { handle.Completed -= AssetContinuation; BaseContinuation(); } - private void SceneContinuation(SceneHandle handle) { handle.Completed -= SceneContinuation; BaseContinuation(); } - private void SubContinuation(SubAssetsHandle handle) { handle.Completed -= SubContinuation; BaseContinuation(); } - private void RawFileContinuation(RawFileHandle handle) { handle.Completed -= RawFileContinuation; BaseContinuation(); } - private void AllAssetsContinuation(AllAssetsHandle handle) - { + { handle.Completed -= AllAssetsContinuation; BaseContinuation(); } @@ -163,83 +143,77 @@ namespace Cysharp.Threading.Tasks [MethodImpl(MethodImplOptions.AggressiveInlining)] private void BaseContinuation() { - if(completed) + if (_completed) { TryReturn(); } else { - completed = true; - if(handle.Status == EOperationStatus.Failed) + _completed = true; + if (_handle.Status == EOperationStatus.Failed) { - core.TrySetException(new Exception(handle.LastError)); + _core.TrySetException(new Exception(_handle.LastError)); } else { - core.TrySetResult(AsyncUnit.Default); + _core.TrySetResult(AsyncUnit.Default); } } } - private void Continuation(HandleBase _) { - switch(handle) + switch (_handle) { case AssetHandle asset_handle: - asset_handle.Completed -= continuationAction; + asset_handle.Completed -= _continuationAction; break; case SceneHandle scene_handle: - scene_handle.Completed -= continuationAction; + scene_handle.Completed -= _continuationAction; break; case SubAssetsHandle sub_asset_handle: - sub_asset_handle.Completed -= continuationAction; + sub_asset_handle.Completed -= _continuationAction; break; case RawFileHandle raw_file_handle: - raw_file_handle.Completed -= continuationAction; + raw_file_handle.Completed -= _continuationAction; break; case AllAssetsHandle all_assets_handle: - all_assets_handle.Completed -= continuationAction; + all_assets_handle.Completed -= _continuationAction; break; } BaseContinuation(); } - - bool TryReturn() + private bool TryReturn() { TaskTracker.RemoveTracking(this); - core.Reset(); - handle = default; - progress = default; - return pool.TryPush(this); + _core.Reset(); + _handle = default; + _progress = default; + return _pool.TryPush(this); } - public UniTaskStatus GetStatus(short token) => core.GetStatus(token); - + public UniTaskStatus GetStatus(short token) => _core.GetStatus(token); public void OnCompleted(Action continuation, object state, short token) { - core.OnCompleted(continuation, state, token); + _core.OnCompleted(continuation, state, token); } - - public void GetResult(short token) { core.GetResult(token); } - - public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus(); - + public void GetResult(short token) { _core.GetResult(token); } + public UniTaskStatus UnsafeGetStatus() => _core.UnsafeGetStatus(); public bool MoveNext() { - if(completed) + if (_completed) { TryReturn(); return false; } - if(handle.IsValid) + if (_handle.IsValid) { - progress?.Report(handle.Progress); + _progress?.Report(_handle.Progress); } return true; } } } -} \ No newline at end of file +}