update UniTask sample

更新说明文档
This commit is contained in:
何冠峰
2025-07-24 14:45:52 +08:00
parent 956b3db71d
commit 2254ac9f5e
9 changed files with 276 additions and 191 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

View File

@@ -0,0 +1,153 @@
fileFormatVersion: 2
guid: d90b9b066363f08419ff51004479d5b7
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 13
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
flipGreenChannel: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMipmapLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 1
mipBias: 0
wrapU: 0
wrapV: 0
wrapW: 0
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
swizzle: 50462976
cookieLightType: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: iPhone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: WebGL
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Android
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
nameFileIdTable: {}
mipmapLimitGroupName:
pSDRemoveMatte: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,57 +1,52 @@
# UniTask 扩展 # UniTask 扩展
这里为了照顾新手使用,做了一些妥协,有定制需求的需要手动调整一下
## 代码示例 ## 代码示例
```csharp ```csharp
public async UniTask Example(IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update) using System;
using UnityEngine;
using YooAsset;
using Cysharp.Threading.Tasks;
public class TestTask : MonoBehaviour
{ {
var handle = YooAssets.LoadAssetAsync<GameObject>("Assets/Res/Prefabs/TestImg.prefab"); private async void Start()
{
await handle.ToUniTask(progress, timing); var assetHandle = YooAssets.LoadAssetAsync<GameObject>("UIHome");
await assetHandle.ToUniTask();
var obj = handle.AssetObject as GameObject; Debug.Log($"{assetHandle.Status}");
var go = Instantiate(obj, transform); }
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
} }
``` ```
## 初学者教程 ## 使用教程
**如果你弄不明白 asmdef 文件到底是啥,就按照下发内容操作** 1. 下载 [UniTask](https://github.com/Cysharp/UniTask) 源码并导入到工程内。
-`Samples/UniTask Sample/UniTask` 文件夹拷入游戏中 2. 修改UniTask源码
- 如果项目有 `asmdef`,则引用 `UniTask``YooAsset`,如果没有,就不用关心这一步
UniTask/Runtime/_InternalVisibleTo.cs
## 项目定制教程 ```csharp
using System.Runtime.CompilerServices;
- 请去下载 [UniTask](https://github.com/Cysharp/UniTask) 源码 [assembly: InternalsVisibleTo("UniTask.Linq")]
- 注意不要用 `Sample` 里面的 `UniTask` 这个是专门给新手定制的 [assembly: InternalsVisibleTo("UniTask.Addressables")]
-`Samples/UniTask Sample/UniTask/Runtime/External/YooAsset` 文件夹拷贝到 `UniTask/Runtime/External/YooAsset` [assembly: InternalsVisibleTo("UniTask.DOTween")]
- 创建 `UniTask.YooAsset.asmdef` 文件 [assembly: InternalsVisibleTo("UniTask.TextMeshPro")]
- 添加 `UniTask``YooAsset` 的引用 [assembly: InternalsVisibleTo("UniTask.YooAsset")] //增加此行代码
- 在 UniTask `_InternalVisibleTo.cs` 文件中增加 `[assembly: InternalsVisibleTo("UniTask.YooAsset")]` 后即可使用
## 有效性检查
一般使用项目定制时, 会出现如下警告, 这说明项目没有配置正确, 建议使用 **初学者定制的** 版本
```
yield BundledSceneProvider is not supported on await IEnumerator or Enumerator. ToUniTaskO, please use ToUniTask MonoBehaviou
coroutine Runner) instead
``` ```
- 在 IDE 中点击 ToUniTask 跳转代码, 看是否可以正确跳转到 `UniTask/Runtime/External/YooAsset` 文件夹中 3. 拷贝YOO提供的扩展脚本到工程内。
- 增加 `handle.ToUniTask(progress, timing)` 参数, 看是否有编译错误
如果不正确, 需要检查业务逻辑的 `asmdef` 是否引用正确, 假设你项目业务逻辑的 `asmdef` 名为 `View.asmdef`, 那么在 `View` 中, 要包含如下引用 YooAssets/Samples/UniTask Sample/UniTask/Runtime/External/YooAsset目录
- YooAsset 4. 重启Unity引擎
- UniTask
- UniTask.YooAsset
如果引用正确, 依然还有报错, 说明定制流程有问题, 请检查定制内容是否正确, 或者使用 **初学者定制的** 版本 重新打开工程后用上面的代码示例验证,看下是否有编译错误!
## 注意事项
- 注意检测程序集引用是否丢失
![image](./README-IMG.png)

View File

@@ -11,10 +11,7 @@ namespace Cysharp.Threading.Tasks
{ {
return ToUniTask(handle).GetAwaiter(); return ToUniTask(handle).GetAwaiter();
} }
public static UniTask ToUniTask(this AsyncOperationBase handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update)
public static UniTask ToUniTask(this AsyncOperationBase handle,
IProgress<float> progress = null,
PlayerLoopTiming timing = PlayerLoopTiming.Update)
{ {
ThrowArgumentNullException(handle, nameof(handle)); ThrowArgumentNullException(handle, nameof(handle));
@@ -24,52 +21,40 @@ namespace Cysharp.Threading.Tasks
} }
return new UniTask( return new UniTask(
AsyncOperationBaserConfiguredSource.Create( AsyncOperationBaserConfiguredSource.Create(handle, timing, progress, out var token),
handle,
timing,
progress,
out var token
),
token token
); );
} }
sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationBaserConfiguredSource>
IPlayerLoopItem,
ITaskPoolNode<AsyncOperationBaserConfiguredSource>
{ {
private static TaskPool<AsyncOperationBaserConfiguredSource> pool; private static TaskPool<AsyncOperationBaserConfiguredSource> _pool;
private AsyncOperationBaserConfiguredSource _nextNode;
private readonly Action<AsyncOperationBase> _continuationAction;
private AsyncOperationBase _handle;
private IProgress<float> _progress;
private bool _completed;
private UniTaskCompletionSourceCore<AsyncUnit> _core;
private AsyncOperationBaserConfiguredSource nextNode; public ref AsyncOperationBaserConfiguredSource NextNode => ref _nextNode;
public ref AsyncOperationBaserConfiguredSource NextNode => ref nextNode;
static AsyncOperationBaserConfiguredSource() static AsyncOperationBaserConfiguredSource()
{ {
TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => pool.Size); TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => _pool.Size);
} }
private readonly Action<AsyncOperationBase> continuationAction; AsyncOperationBaserConfiguredSource() { _continuationAction = Continuation; }
private AsyncOperationBase handle;
private IProgress<float> progress;
private bool completed;
private UniTaskCompletionSourceCore<AsyncUnit> core;
AsyncOperationBaserConfiguredSource() { continuationAction = Continuation; } public static IUniTaskSource Create(AsyncOperationBase handle, PlayerLoopTiming timing, IProgress<float> progress, out short token)
public static IUniTaskSource Create(AsyncOperationBase handle,
PlayerLoopTiming timing,
IProgress<float> progress,
out short token)
{ {
if(!pool.TryPop(out var result)) if (!_pool.TryPop(out var result))
{ {
result = new AsyncOperationBaserConfiguredSource(); result = new AsyncOperationBaserConfiguredSource();
} }
result.handle = handle; result._handle = handle;
result.progress = progress; result._progress = progress;
result.completed = false; result._completed = false;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);
if (progress != null) if (progress != null)
@@ -77,66 +62,59 @@ namespace Cysharp.Threading.Tasks
PlayerLoopHelper.AddAction(timing, result); PlayerLoopHelper.AddAction(timing, result);
} }
handle.Completed += result.continuationAction; handle.Completed += result._continuationAction;
token = result._core.Version;
token = result.core.Version;
return result; return result;
} }
private void Continuation(AsyncOperationBase _) private void Continuation(AsyncOperationBase _)
{ {
handle.Completed -= continuationAction; _handle.Completed -= _continuationAction;
if(completed) if (_completed)
{ {
TryReturn(); TryReturn();
} }
else else
{ {
completed = true; _completed = true;
if(handle.Status == EOperationStatus.Failed) if (_handle.Status == EOperationStatus.Failed)
{ {
core.TrySetException(new Exception(handle.Error)); _core.TrySetException(new Exception(_handle.Error));
} }
else else
{ {
core.TrySetResult(AsyncUnit.Default); _core.TrySetResult(AsyncUnit.Default);
} }
} }
} }
private bool TryReturn()
bool TryReturn()
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
core.Reset(); _core.Reset();
handle = default; _handle = default;
progress = default; _progress = default;
return pool.TryPush(this); return _pool.TryPush(this);
} }
public UniTaskStatus GetStatus(short token) => core.GetStatus(token); public UniTaskStatus GetStatus(short token) => _core.GetStatus(token);
public void OnCompleted(Action<object> continuation, object state, short token) public void OnCompleted(Action<object> continuation, object state, short token)
{ {
core.OnCompleted(continuation, state, token); _core.OnCompleted(continuation, state, token);
} }
public void GetResult(short token) { _core.GetResult(token); }
public void GetResult(short token) { core.GetResult(token); } public UniTaskStatus UnsafeGetStatus() => _core.UnsafeGetStatus();
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public bool MoveNext() public bool MoveNext()
{ {
if(completed) if (_completed)
{ {
TryReturn(); TryReturn();
return false; return false;
} }
if(!handle.IsDone) if (!_handle.IsDone)
{ {
progress?.Report(handle.Progress); _progress?.Report(_handle.Progress);
} }
return true; return true;

View File

@@ -8,19 +8,15 @@ using System.Runtime.CompilerServices;
using YooAsset; using YooAsset;
using static Cysharp.Threading.Tasks.Internal.Error; using static Cysharp.Threading.Tasks.Internal.Error;
namespace Cysharp.Threading.Tasks namespace Cysharp.Threading.Tasks
{ {
public static class OperationHandleBaseExtensions public static class HandleBaseExtensions
{ {
public static UniTask.Awaiter GetAwaiter(this HandleBase handle) public static UniTask.Awaiter GetAwaiter(this HandleBase handle)
{ {
return ToUniTask(handle).GetAwaiter(); return ToUniTask(handle).GetAwaiter();
} }
public static UniTask ToUniTask(this HandleBase handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update)
public static UniTask ToUniTask(this HandleBase handle,
IProgress<float> progress = null,
PlayerLoopTiming timing = PlayerLoopTiming.Update)
{ {
ThrowArgumentNullException(handle, nameof(handle)); ThrowArgumentNullException(handle, nameof(handle));
@@ -30,52 +26,40 @@ namespace Cysharp.Threading.Tasks
} }
return new UniTask( return new UniTask(
OperationHandleBaserConfiguredSource.Create( HandleBaserConfiguredSource.Create(handle, timing, progress, out var token),
handle,
timing,
progress,
out var token
),
token token
); );
} }
sealed class OperationHandleBaserConfiguredSource : IUniTaskSource, sealed class HandleBaserConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<HandleBaserConfiguredSource>
IPlayerLoopItem,
ITaskPoolNode<OperationHandleBaserConfiguredSource>
{ {
private static TaskPool<OperationHandleBaserConfiguredSource> pool; private static TaskPool<HandleBaserConfiguredSource> _pool;
private HandleBaserConfiguredSource _nextNode;
private readonly Action<HandleBase> _continuationAction;
private HandleBase _handle;
private IProgress<float> _progress;
private bool _completed;
private UniTaskCompletionSourceCore<AsyncUnit> _core;
private OperationHandleBaserConfiguredSource nextNode; public ref HandleBaserConfiguredSource NextNode => ref _nextNode;
public ref OperationHandleBaserConfiguredSource NextNode => ref nextNode; static HandleBaserConfiguredSource()
static OperationHandleBaserConfiguredSource()
{ {
TaskPool.RegisterSizeGetter(typeof(OperationHandleBaserConfiguredSource), () => pool.Size); TaskPool.RegisterSizeGetter(typeof(HandleBaserConfiguredSource), () => _pool.Size);
} }
private readonly Action<HandleBase> continuationAction; HandleBaserConfiguredSource() { _continuationAction = Continuation; }
private HandleBase handle;
private IProgress<float> progress;
private bool completed;
private UniTaskCompletionSourceCore<AsyncUnit> core;
OperationHandleBaserConfiguredSource() { continuationAction = Continuation; } public static IUniTaskSource Create(HandleBase handle, PlayerLoopTiming timing, IProgress<float> progress, out short token)
public static IUniTaskSource Create(HandleBase handle,
PlayerLoopTiming timing,
IProgress<float> 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._handle = handle;
result.progress = progress; result._progress = progress;
result.completed = false; result._completed = false;
TaskTracker.TrackActiveTask(result, 3); TaskTracker.TrackActiveTask(result, 3);
if (progress != null) if (progress != null)
@@ -126,35 +110,31 @@ namespace Cysharp.Threading.Tasks
break; break;
} }
#endif #endif
token = result.core.Version; token = result._core.Version;
return result; return result;
} }
#if UNITY_2020_BUG #if UNITY_2020_BUG
private void AssetContinuation(AssetHandle handle) private void AssetContinuation(AssetHandle handle)
{ {
handle.Completed -= AssetContinuation; handle.Completed -= AssetContinuation;
BaseContinuation(); BaseContinuation();
} }
private void SceneContinuation(SceneHandle handle) private void SceneContinuation(SceneHandle handle)
{ {
handle.Completed -= SceneContinuation; handle.Completed -= SceneContinuation;
BaseContinuation(); BaseContinuation();
} }
private void SubContinuation(SubAssetsHandle handle) private void SubContinuation(SubAssetsHandle handle)
{ {
handle.Completed -= SubContinuation; handle.Completed -= SubContinuation;
BaseContinuation(); BaseContinuation();
} }
private void RawFileContinuation(RawFileHandle handle) private void RawFileContinuation(RawFileHandle handle)
{ {
handle.Completed -= RawFileContinuation; handle.Completed -= RawFileContinuation;
BaseContinuation(); BaseContinuation();
} }
private void AllAssetsContinuation(AllAssetsHandle handle) private void AllAssetsContinuation(AllAssetsHandle handle)
{ {
handle.Completed -= AllAssetsContinuation; handle.Completed -= AllAssetsContinuation;
@@ -164,79 +144,73 @@ namespace Cysharp.Threading.Tasks
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void BaseContinuation() private void BaseContinuation()
{ {
if(completed) if (_completed)
{ {
TryReturn(); TryReturn();
} }
else else
{ {
completed = true; _completed = true;
if(handle.Status == EOperationStatus.Failed) if (_handle.Status == EOperationStatus.Failed)
{ {
core.TrySetException(new Exception(handle.LastError)); _core.TrySetException(new Exception(_handle.LastError));
} }
else else
{ {
core.TrySetResult(AsyncUnit.Default); _core.TrySetResult(AsyncUnit.Default);
} }
} }
} }
private void Continuation(HandleBase _) private void Continuation(HandleBase _)
{ {
switch(handle) switch (_handle)
{ {
case AssetHandle asset_handle: case AssetHandle asset_handle:
asset_handle.Completed -= continuationAction; asset_handle.Completed -= _continuationAction;
break; break;
case SceneHandle scene_handle: case SceneHandle scene_handle:
scene_handle.Completed -= continuationAction; scene_handle.Completed -= _continuationAction;
break; break;
case SubAssetsHandle sub_asset_handle: case SubAssetsHandle sub_asset_handle:
sub_asset_handle.Completed -= continuationAction; sub_asset_handle.Completed -= _continuationAction;
break; break;
case RawFileHandle raw_file_handle: case RawFileHandle raw_file_handle:
raw_file_handle.Completed -= continuationAction; raw_file_handle.Completed -= _continuationAction;
break; break;
case AllAssetsHandle all_assets_handle: case AllAssetsHandle all_assets_handle:
all_assets_handle.Completed -= continuationAction; all_assets_handle.Completed -= _continuationAction;
break; break;
} }
BaseContinuation(); BaseContinuation();
} }
private bool TryReturn()
bool TryReturn()
{ {
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
core.Reset(); _core.Reset();
handle = default; _handle = default;
progress = default; _progress = default;
return pool.TryPush(this); return _pool.TryPush(this);
} }
public UniTaskStatus GetStatus(short token) => core.GetStatus(token); public UniTaskStatus GetStatus(short token) => _core.GetStatus(token);
public void OnCompleted(Action<object> continuation, object state, short token) public void OnCompleted(Action<object> continuation, object state, short token)
{ {
core.OnCompleted(continuation, state, token); _core.OnCompleted(continuation, state, token);
} }
public void GetResult(short token) { _core.GetResult(token); }
public void GetResult(short token) { core.GetResult(token); } public UniTaskStatus UnsafeGetStatus() => _core.UnsafeGetStatus();
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public bool MoveNext() public bool MoveNext()
{ {
if(completed) if (_completed)
{ {
TryReturn(); TryReturn();
return false; return false;
} }
if(handle.IsValid) if (_handle.IsValid)
{ {
progress?.Report(handle.Progress); _progress?.Report(_handle.Progress);
} }
return true; return true;

View File

@@ -1,3 +0,0 @@
{
"reference": "GUID:f51ebe6a0ceec4240a699833d6309b23"
}

View File

@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: a1f496f94e051be4b8cf9c5efd954c14
AssemblyDefinitionReferenceImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,3 +0,0 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("UniTask.YooAsset")]

View File

@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 6d04a1d7d6171ff438c8e68a007be014