Skip to content

Commit

Permalink
Add more tasks to UnityTaskExtensions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
PereViader committed Jun 24, 2024
1 parent 5f4e7ef commit 51d1613
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
using System.Threading.Tasks;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace PereViader.Utils.Unity3d.Extensions
{
public static class UnityTaskExtensions
{
public static async void RunAsync(this Task task)
{
await task;
try
{
await task;
}
catch (OperationCanceledException)
{
}
}

public static async Task WaitUntil(this Func<bool> func, CancellationToken ct)
{
while (!func())
{
await Task.Yield();
ct.ThrowIfCancellationRequested();
}
}

public static async Task WaitWhile(this Func<bool> func, CancellationToken ct)
{
while (func())
{
await Task.Yield();
ct.ThrowIfCancellationRequested();
}
}

public static async Task WaitFrame(CancellationToken ct)
{
await Task.Yield();
ct.ThrowIfCancellationRequested();
}

public static async Task WaitFrames(int frameCount, CancellationToken ct)
{
for (int i = 0; i < frameCount; i++)
{
await Task.Yield();
ct.ThrowIfCancellationRequested();
}
}
}
}

0 comments on commit 51d1613

Please sign in to comment.