-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more tasks to UnityTaskExtensions.cs
- Loading branch information
1 parent
5f4e7ef
commit 51d1613
Showing
1 changed file
with
43 additions
and
2 deletions.
There are no files selected for viewing
45 changes: 43 additions & 2 deletions
45
...Unity3d/Assets/PereViader.Utils.Unity3d/Scripts/Runtime/Extensions/UnityTaskExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |