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

View File

@@ -11,132 +11,110 @@ namespace Cysharp.Threading.Tasks
{
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));
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<AsyncOperationBaserConfiguredSource>
sealed class AsyncOperationBaserConfiguredSource : IUniTaskSource, 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()
{
TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => pool.Size);
TaskPool.RegisterSizeGetter(typeof(AsyncOperationBaserConfiguredSource), () => _pool.Size);
}
private readonly Action<AsyncOperationBase> continuationAction;
private AsyncOperationBase handle;
private IProgress<float> progress;
private bool completed;
private UniTaskCompletionSourceCore<AsyncUnit> core;
AsyncOperationBaserConfiguredSource() { _continuationAction = Continuation; }
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.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<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 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;

View File

@@ -8,77 +8,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<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));
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<OperationHandleBaserConfiguredSource>
sealed class HandleBaserConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<HandleBaserConfiguredSource>
{
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 OperationHandleBaserConfiguredSource()
static HandleBaserConfiguredSource()
{
TaskPool.RegisterSizeGetter(typeof(OperationHandleBaserConfiguredSource), () => pool.Size);
TaskPool.RegisterSizeGetter(typeof(HandleBaserConfiguredSource), () => _pool.Size);
}
private readonly Action<HandleBase> continuationAction;
private HandleBase handle;
private IProgress<float> progress;
private bool completed;
private UniTaskCompletionSourceCore<AsyncUnit> core;
HandleBaserConfiguredSource() { _continuationAction = Continuation; }
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.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);
}
@@ -88,7 +72,7 @@ namespace Cysharp.Threading.Tasks
// BUG 也可能报的是 Action '1' Action '1' 的 InvalidCastException
// BUG 此处不得不这么修改, 如果后续 Unity 修复了这个问题, 可以恢复之前的写法
#if UNITY_2020_BUG
switch(handle)
switch (handle)
{
case AssetHandle asset_handle:
asset_handle.Completed += result.AssetContinuation;
@@ -126,37 +110,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();
}
@@ -164,79 +144,73 @@ 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<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 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;

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