Unity中UniTask如何取消WhenAny | WhenAll的等待?

异步等待的取消,是一个很重要的事情,那么UniTask中如何取消WhenAny和WhenAll的异步等待呢?

  • 使用 扩展方法:AttachExternalCancellation()
  • 案例:
 public async UniTask FlowAsync(CancellationToken ctk)
    {
        var allTasks = scripts.Select(s => (s as IFlowAsync).FlowAsync(ctk));
        await UniTask.WhenAll(allTasks).AttachExternalCancellation(ctk);
        await UniTask.WhenAny(allTasks).AttachExternalCancellation(ctk);
    }

你可能感兴趣的:(unity,UniTask,WhenAll,WhenAny,cancellation)