Last active
October 9, 2023 20:54
-
-
Save ShuraProgerMain/0e95cf1b4b9ec1f31d9142cf6eec3613 to your computer and use it in GitHub Desktop.
GraduateHelper
This file contains hidden or 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
using System; | |
using System.Threading.Tasks; | |
using UnityEngine; | |
namespace Helpers | |
{ | |
public static class GraduateHelper | |
{ | |
public static async void Graduate(Action<float> action, float duration, bool reverse = false) | |
{ | |
try | |
{ | |
await GraduateAsync(action, duration, reverse); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
throw; | |
} | |
} | |
public static async Task GraduateAsync(Action<float> action, float duration, bool reverse = false) | |
{ | |
for (var time = 0f; time < duration; time += Time.deltaTime) | |
{ | |
var ratio = time / duration; | |
ratio = reverse ? 1f - ratio : ratio; | |
var progress = ratio; | |
action.Invoke(progress); | |
await Task.Yield(); | |
} | |
action.Invoke(reverse ? 0f : 1f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment