Supplemental codes for UniTask.
Mainly adds shorthand methods that do not require cancellationToken:
.
// UniTask
await UniTask.DelayFrame(5, cancellationToken: cancellationToken);
// with UniTask Supplement
await UniTask.DelayFrame(5, cancellationToken);
// UniTask
await UniTask.Delay(500, cancellationToken: cancellationToken);
// with UniTask Supplement
await UniTask.Delay(500, cancellationToken);
// UniTask
await UniTask.Delay(TimeSpan.FromMilliseconds(500), cancellationToken: cancellationToken);
// with UniTask Supplement
await UniTask.Delay(TimeSpan.FromMilliseconds(500), cancellationToken);
// UniTask
await UniTask.Delay(500, cancellationToken: cancellationToken);
// with UniTask Supplement
await UniTask.DelayMilliseconds(500, cancellationToken);
You can disable DelayMilliseconds()
to define UNITASK_SUPPLEMENT_DISABLE_DELAY_MILLISECONDS
.
// UniTask
await UniTask.Delay(1500, cancellationToken: cancellationToken);
await UniTask.Delay(TimeSpan.FromSeconds(1.5f), cancellationToken: cancellationToken);
// with UniTask Supplement
await UniTask.DelaySeconds(1.5f, cancellationToken);
You can disable DelaySeconds()
to define UNITASK_SUPPLEMENT_DISABLE_DELAY_SECONDS
.
// UniTask
await UniTask.WaitUntil(() => flag, cancellationToken: cancellationToken);
// with UniTask Supplement
await UniTask.WaitUntil(() => flag, cancellationToken);
// UniTask
await UniTask.WaitWhile(() => flag, cancellationToken: cancellationToken);
// with UniTask Supplement
await UniTask.WaitWhile(() => flag, cancellationToken);
// UniTask
await UniTask.WaitUntilValueChanged(transform, x => x.position, cancellationToken: cancellationToken);
// with UniTask Supplement
await UniTask.WaitUntilValueChanged(transform, x => x.position, cancellationToken);
// UniTask
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
(bool hasResultLeft, T result) result;
try
{
result = await WhenAny(TaskA(cts.Token), TaskB(cts.Token));
cts.Cancel();
}
catch (OperationCanceledException ex) when (ex.CancellationToken == cts.Token)
{
if (cancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException(ex.Message, ex, cancellationToken);
}
throw;
}
finally
{
cts.Dispose();
}
...
}
// with UniTask Supplement
var (hasResultLeft, result) = await UniTask.WhenAny<T>(
cancel => TaskA(cancel),
cancel => TaskB(cancel),
cancellationToken
);
// UniTask
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
(int winArgumentIndex, T result) result = default;
try
{
result = await WhenAny<T>(
taskFunc1(cts.Token),
taskFunc2(cts.Token),
..
);
cts.Cancel();
}
catch (OperationCanceledException ex) when (ex.CancellationToken == cts.Token)
{
if (cancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException(ex.Message, ex, cancellationToken);
}
throw;
}
finally
{
cts.Dispose();
}
...
}
// with UniTask Supplement
var (winArgumentIndex, result) = await UniTask.WhenAny<T>(
cancel => TaskA(cancel),
cancel => TaskB(cancel),
..,
cancellationToken
);
You can disable WhenAny(.., cancellationToken)
to define UNITASK_SUPPLEMENT_DISABLE_WHEN_ANY
.
// UniTask
await tween.ToUniTask(cancellationToken: cancellationToken);
await tween.AwaitForComplete(cancellationToken: cancellationToken);
await tween.AwaitForPause(cancellationToken: cancellationToken);
await tween.AwaitForPlay(cancellationToken: cancellationToken);
await tween.AwaitForRewind(cancellationToken: cancellationToken);
await tween.AwaitForStepComplete(cancellationToken: cancellationToken);
// with UniTask Supplement
await tween.ToUniTask(cancellationToken);
await tween.AwaitForComplete(cancellationToken);
await tween.AwaitForPause(cancellationToken);
await tween.AwaitForPlay(cancellationToken);
await tween.AwaitForRewind(cancellationToken);
await tween.AwaitForStepComplete(cancellationToken);
You can enable this behavior to define UNITASK_SUPPLEMENT_DOTWEEN_SUPPORT
instead of UNITASK_DOTWEEN_SUPPORT
.
See this thread (Twitter) for why this should be.
// UniTask
// OperationCanceledException will not be thrown when canceled.
await tween.WithCancellation(cancellationToken);
await tween.ToUniTask(cancellationToken: cancellationToken);
await tween.AwaitForComplete(cancellationToken: cancellationToken);
await tween.AwaitForPause(cancellationToken: cancellationToken);
await tween.AwaitForPlay(cancellationToken: cancellationToken);
await tween.AwaitForRewind(cancellationToken: cancellationToken);
await tween.AwaitForStepComplete(cancellationToken: cancellationToken);
// but with UniTask Supplement, OperationCanceledException will be thrown when canceled.
// UniTask
await tween.ToUniTask(TweenCancelBehaviour.KillAndCancelAwait, cancellationToken);
await tween.AwaitForComplete(TweenCancelBehaviour.KillAndCancelAwait, cancellationToken);
await tween.AwaitForPause(TweenCancelBehaviour.KillAndCancelAwait, cancellationToken);
await tween.AwaitForPlay(TweenCancelBehaviour.KillAndCancelAwait, cancellationToken);
await tween.AwaitForRewind(TweenCancelBehaviour.KillAndCancelAwait, cancellationToken);
await tween.AwaitForStepComplete(TweenCancelBehaviour.KillAndCancelAwait, cancellationToken);
// with UniTask Supplement
await tween.ToUniTask(cancellationToken);
await tween.AwaitForComplete(cancellationToken);
await tween.AwaitForPause(cancellationToken);
await tween.AwaitForPlay(cancellationToken);
await tween.AwaitForRewind(cancellationToken);
await tween.AwaitForStepComplete(cancellationToken);
You can restore original behavior to define UNITASK_SUPPLEMENT_DOTWEEN_SUPPORT_USE_ORIGINAL_DEFAULT_TWEEN_CANCEL_BEHAVIOUR
.
The package is available on the openupm registry. It's recommended to install it via openupm-cli.
openupm add com.jagapippi.unitask-supplement
- Open the Package Manager
- Press [+▼] button and click Add package from git URL...
- Enter the following:
or add a following line to dependencies field of your Packages/manifest.json.
"com.jagapippi.unitask-supplement": "https://github.com/su10/UniTask-Supplement.git#upm"
MIT