From 55bcd502e23e490555e064f76e6bdf2f7050cbfc Mon Sep 17 00:00:00 2001 From: L <401419353@qq.com> Date: Mon, 25 Apr 2022 15:35:11 +0800 Subject: [PATCH] Add UniTask Support --- .../OperationHandleBaseExtensions.cs | 167 ++++++++++++++++++ .../OperationHandleBaseExtensions.cs.meta | 3 + Assets/UniTask.YooAsset~/README.md | 19 ++ .../UniTask.YooAsset~/UniTask.YooAsset.asmdef | 17 ++ .../UniTask.YooAsset.asmdef.meta | 7 + 5 files changed, 213 insertions(+) create mode 100644 Assets/UniTask.YooAsset~/OperationHandleBaseExtensions.cs create mode 100644 Assets/UniTask.YooAsset~/OperationHandleBaseExtensions.cs.meta create mode 100644 Assets/UniTask.YooAsset~/README.md create mode 100644 Assets/UniTask.YooAsset~/UniTask.YooAsset.asmdef create mode 100644 Assets/UniTask.YooAsset~/UniTask.YooAsset.asmdef.meta diff --git a/Assets/UniTask.YooAsset~/OperationHandleBaseExtensions.cs b/Assets/UniTask.YooAsset~/OperationHandleBaseExtensions.cs new file mode 100644 index 0000000..f05f28f --- /dev/null +++ b/Assets/UniTask.YooAsset~/OperationHandleBaseExtensions.cs @@ -0,0 +1,167 @@ +using System; +using YooAsset; +using static Cysharp.Threading.Tasks.Internal.Error; + +namespace Cysharp.Threading.Tasks +{ + public static class OperationHandleBaseExtensions + { + public static UniTask.Awaiter GetAwaiter(this OperationHandleBase handle) + { + return ToUniTask(handle).GetAwaiter(); + } + + public static UniTask ToUniTask(this OperationHandleBase handle, + IProgress progress = null, + PlayerLoopTiming timing = PlayerLoopTiming.Update) + { + ThrowArgumentNullException(handle, nameof(handle)); + + if(!handle.IsValid) + { + return UniTask.CompletedTask; + } + + return new UniTask( + OperationHandleBaserConfiguredSource.Create( + handle, + timing, + progress, + out var token + ), + token + ); + } + + sealed class OperationHandleBaserConfiguredSource : IUniTaskSource, + IPlayerLoopItem, + ITaskPoolNode + { + private static TaskPool pool; + + private OperationHandleBaserConfiguredSource nextNode; + + public ref OperationHandleBaserConfiguredSource NextNode => ref nextNode; + + static OperationHandleBaserConfiguredSource() + { + TaskPool.RegisterSizeGetter(typeof(OperationHandleBaserConfiguredSource), () => pool.Size); + } + + private readonly Action continuationAction; + private OperationHandleBase handle; + private IProgress progress; + private bool completed; + private UniTaskCompletionSourceCore core; + + OperationHandleBaserConfiguredSource() { continuationAction = Continuation; } + + public static IUniTaskSource Create(OperationHandleBase handle, + PlayerLoopTiming timing, + IProgress progress, + out short token) + { + if(!pool.TryPop(out var result)) + { + result = new OperationHandleBaserConfiguredSource(); + } + + result.handle = handle; + result.progress = progress; + result.completed = false; + TaskTracker.TrackActiveTask(result, 3); + + if(progress is not null) + { + PlayerLoopHelper.AddAction(timing, result); + } + + switch(handle) + { + case AssetOperationHandle asset_handle: + asset_handle.Completed += result.continuationAction; + break; + case SceneOperationHandle scene_handle: + scene_handle.Completed += result.continuationAction; + break; + case SubAssetsOperationHandle sub_asset_handle: + sub_asset_handle.Completed += result.continuationAction; + break; + } + + token = result.core.Version; + + return result; + } + + private void Continuation(OperationHandleBase _) + { + switch(handle) + { + case AssetOperationHandle asset_handle: + asset_handle.Completed -= continuationAction; + break; + case SceneOperationHandle scene_handle: + scene_handle.Completed -= continuationAction; + break; + case SubAssetsOperationHandle sub_asset_handle: + sub_asset_handle.Completed -= continuationAction; + break; + } + + if(completed) + { + TryReturn(); + } + else + { + completed = true; + if(handle.Status == EOperationStatus.Failed) + { + core.TrySetException(new Exception(handle.LastError)); + } + else + { + core.TrySetResult(AsyncUnit.Default); + } + } + } + + bool TryReturn() + { + TaskTracker.RemoveTracking(this); + core.Reset(); + handle = default; + progress = default; + return pool.TryPush(this); + } + + public UniTaskStatus GetStatus(short token) => core.GetStatus(token); + + public void OnCompleted(Action continuation, object state, short token) + { + core.OnCompleted(continuation, state, token); + } + + public void GetResult(short token) { core.GetResult(token); } + + public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus(); + + public bool MoveNext() + { + if(completed) + { + TryReturn(); + return false; + } + + if(handle.IsValid) + { + progress?.Report(handle.Progress); + } + + return true; + } + } + } +} \ No newline at end of file diff --git a/Assets/UniTask.YooAsset~/OperationHandleBaseExtensions.cs.meta b/Assets/UniTask.YooAsset~/OperationHandleBaseExtensions.cs.meta new file mode 100644 index 0000000..b43f66e --- /dev/null +++ b/Assets/UniTask.YooAsset~/OperationHandleBaseExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e1c9a3a6de2246bf88547a6b59b99b9f +timeCreated: 1650851321 \ No newline at end of file diff --git a/Assets/UniTask.YooAsset~/README.md b/Assets/UniTask.YooAsset~/README.md new file mode 100644 index 0000000..790c19b --- /dev/null +++ b/Assets/UniTask.YooAsset~/README.md @@ -0,0 +1,19 @@ +# UniTask 扩展 + +[仓库链接](https://github.com/Cysharp/UniTask) +- 请去下载对应的源码,并删除此目录最后的波浪线 +- 在 UniTask `_InternalVisibleTo.cs` 文件中增加 `[assembly: InternalsVisibleTo("UniTask.YooAsset")]` 后即可使用 + +## 代码示例 + +```csharp +var handle = YooAssets.LoadAssetAsync("Assets/Res/Prefabs/TestImg.prefab"); + +await handle.ToUniTask(); + +var obj = handle.AssetObject as GameObject; +var go = Instantiate(obj, transform); + +go.transform.localPosition = Vector3.zero; +go.transform.localScale = Vector3.one; +``` \ No newline at end of file diff --git a/Assets/UniTask.YooAsset~/UniTask.YooAsset.asmdef b/Assets/UniTask.YooAsset~/UniTask.YooAsset.asmdef new file mode 100644 index 0000000..8fc6583 --- /dev/null +++ b/Assets/UniTask.YooAsset~/UniTask.YooAsset.asmdef @@ -0,0 +1,17 @@ +{ + "name": "UniTask.YooAsset", + "rootNamespace": "", + "references": [ + "GUID:e34a5702dd353724aa315fb8011f08c3", + "GUID:f51ebe6a0ceec4240a699833d6309b23" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/UniTask.YooAsset~/UniTask.YooAsset.asmdef.meta b/Assets/UniTask.YooAsset~/UniTask.YooAsset.asmdef.meta new file mode 100644 index 0000000..5b7ea8e --- /dev/null +++ b/Assets/UniTask.YooAsset~/UniTask.YooAsset.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: cf4f1d4730c114c48ab680458e5a2455 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: