UniTask
安装
git: https://github.com/Cysharp/UniTask
https://gitee.com/cloudlin/UniTask.git
可以直接下载包进行导入: https://github.com/Cysharp/UniTask/releases
也可以通过PackageManager添加第3方git插件
常见用法
// extension awaiter/methods can be used by this namespace
using Cysharp.Threading.Tasks;
// You can return type as struct UniTask<T>(or UniTask), it is unity specialized lightweight alternative of Task<T>
// zero allocation and fast excution for zero overhead async/await integrate with Unity
// 所有任务都要能接受1个 CancellationToken 参数用来取消
async UniTask<string> Test1(CancellationToken cancellationToken)
{
// 可以等待所有 AsyncOperation Request IEnumerator Task
// 等待异步操作系列,底层分2种实现,不能取消的直接通过 asyncOperation.completed += 注册事件
// 能取消的会转成每帧检测,所以能更新进度,检测到 asyncOperation.isDone 时就完成
// 参考 UnityAsyncExtensions.cs AsyncOperationConfiguredSource.MoveNext
var asset = await Resources.LoadAsync<TextAsset>("foo");
var txt = (await UnityWebRequest.Get("https://...").SendWebRequest()).downloadHandler.text;
// await LoadSceneAsync().ToUniTask() 会导致场景中脚本的 Start 函数和 await 后代码的顺序问题
// 因为 ToUniTask 和 WithCancellation 使用不同于 await AsyncOperation 的定时方式,
// 因此加载场景后面不要加 .ToUniTask 和 .WithCancellation
await SceneManager.LoadSceneAsync("scene2");
// 如果你想处理取消请求,可以 LoadSceneAsync 在之后调用 ThrowIfCancellationRequested
// 等价于 if (IsCancellationRequested)ThrowOperationCanceledException();
cancellationToken.ThrowIfCancellationRequested();
// 在 async 函数中你可以主动抛出 CanceledException 异常来取消函数执行
throw new OperationCanceledException();
// .WithCancellation enables Cancel, GetCancellationTokenOnDestroy synchornizes with lifetime of GameObject
var asset2 = await Resources.LoadAsync<TextAsset>("bar").WithCancellation(this.GetCancellationTokenOnDestroy());
// .ToUniTask accepts progress callback(and all options), Progress.Create is a lightweight alternative of IProgress<T>
var asset3 = await Resources.LoadAsync<TextAsset>("baz").ToUniTask(Progress.Create<float>(x => Debug.Log(x)));
// get async webrequest
async UniTask<string> GetTextAsync(UnityWebRequest req)
{
var op = await req.SendWebRequest();
return op.downloadHandler.text;
}
var task1 = GetTextAsync(UnityWebRequest.Get("http://google.com"));
var task2 = GetTextAsync(UnityWebRequest.Get("http://bing.com"));
var task3 = GetTextAsync(UnityWebRequest.Get("http://yahoo.com"));
// 等待所有,注意这个没有取消操作??
// 参考 UniTask.WhenAll.cs
// concurrent async-wait and get results easily by tuple syntax
var (google, bing, yahoo) = await UniTask.WhenAll(task1, task2, task3);
// shorthand of WhenAll, tuple can await directly
var (google2, bing2, yahoo2) = await (task1, task2, task3);
// 等待任意,注意这个没有取消操作??,而且当1个完成时其它的不会取消等待,只是其它的完成后不会再调用回调而已
//var ret = await UniTask.WhenAny(task1, task2, task3);
// 等待时间系列,底层全部是每帧进行判断,到时间就执行
// 默认不会自动取消,当你传入 cancellationToken 并触发取消时并不会马上取消监听
// 而是在下一帧(不管等待多少帧或多少秒)更新时,检测到 cancellationToken.IsCancellationRequested 返回false才会移除监听器
// 参考 UniTask.Delays.cs DelayPromise.MoveNext
// await frame-based operation like a coroutine
await UniTask.DelayFrame