Created
June 6, 2021 02:50
-
-
Save dyguests/9f2fa68724da7e1d59bdad4ecd8b9af5 to your computer and use it in GitHub Desktop.
AnimationCurve PlayEvaluate
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
using System; | |
using System.Collections; | |
using UnityEngine; | |
namespace Util | |
{ | |
public static class AnimationCurveEx | |
{ | |
public static IEnumerator PlayEvaluate(this AnimationCurve curve, float time, Action<float> action, params Tuple<float, Action>[] animationEvents) | |
{ | |
var lastProgress = 0f; | |
var costTime = 0f; | |
while (costTime < time) | |
{ | |
lastProgress = costTime / time; | |
costTime += Time.deltaTime; | |
var progress = costTime / time; | |
var value = curve.Evaluate(progress); | |
action(value); | |
foreach (var animationEvent in animationEvents) | |
{ | |
if (lastProgress <= animationEvent.Item1 && progress >= animationEvent.Item1) | |
{ | |
animationEvent.Item2(); | |
} | |
} | |
yield return null; | |
} | |
action(curve.Evaluate(1f)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment