Manual/async-await-support #722
Replies: 1 comment 1 reply
-
When cancelling an awaitable method from the // If the destroyCancellationToken is raised, cancelling the WaitForSecondsAsync method, then the exception will be throw to the Unity console.
await Awaitable.WaitForSecondsAsync(1f, destroyCancellationToken);
// While not ideal, one way to avoid this exception being written to the console is to intercept it.
try
{
await Awaitable.WaitForSecondsAsync(1f, destroyCancellationToken);
}
catch (OperationCanceledException) { } Here is a utility class that does that and a bit more: public static class Wait
{
public static async Awaitable Duration(float seconds, CancellationToken token = default)
{
try
{
await Awaitable.WaitForSecondsAsync(seconds, token);
}
catch (OperationCanceledException) { }
}
public static async Awaitable NextFrame(CancellationToken token = default)
{
try
{
await Awaitable.NextFrameAsync(token);
}
catch (OperationCanceledException) { }
}
/// <summary>
/// Wait for more than 1 frame.
/// </summary>
public static async Awaitable NextFrame(int frames, CancellationToken token = default)
{
for (int i = 0; i < frames; i++)
{
if (token.IsCancellationRequested)
{
break;
}
await Awaitable.NextFrameAsync(token);
}
}
public static async Awaitable While(Func<bool> predicate, CancellationToken token = default)
{
while (!token.IsCancellationRequested && predicate())
{
await NextFrame(token);
}
}
public static async Awaitable Until(Func<bool> predicate, CancellationToken token = default)
{
while (!token.IsCancellationRequested && !predicate())
{
await NextFrame(token);
}
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Manual/async-await-support
https://docs.unity3d.com/Manual/async-await-support.html
Beta Was this translation helpful? Give feedback.
All reactions