diff --git a/Runtime/Utility/TaskUtility.cs b/Runtime/Utility/TaskUtility.cs index 5f5e620..c110f55 100644 --- a/Runtime/Utility/TaskUtility.cs +++ b/Runtime/Utility/TaskUtility.cs @@ -1,18 +1,33 @@ using System; using System.Threading; using System.Threading.Tasks; +using UnityEngine; namespace Better.Commons.Runtime.Utility { public static class TaskUtility { - public static Task WaitForSeconds(float seconds, CancellationToken cancellationToken = default) + public static async Task WaitForSeconds(float seconds, CancellationToken cancellationToken = default) + { + if (seconds <= 0 || cancellationToken.IsCancellationRequested) + { + return; + } + + while (seconds > 0 && !cancellationToken.IsCancellationRequested) + { + await Task.Yield(); + seconds -= Time.unscaledDeltaTime; + } + } + + public static Task Delay(float seconds, CancellationToken cancellationToken = default) { if (seconds <= 0 || cancellationToken.IsCancellationRequested) { return Task.CompletedTask; } - + var millisecondsDelay = TimeUtility.SecondsToMilliseconds(seconds); return Task.Delay(millisecondsDelay, cancellationToken); } diff --git a/package.json b/package.json index bc4cbb4..bbf929d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.commons", "displayName": "Better Commons", - "version": "0.0.41", + "version": "0.0.42", "unity": "2021.3", "description": " ", "dependencies": {