Skip to content

Instantly share code, notes, and snippets.

@dyguests
Created June 6, 2021 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyguests/9f2fa68724da7e1d59bdad4ecd8b9af5 to your computer and use it in GitHub Desktop.
Save dyguests/9f2fa68724da7e1d59bdad4ecd8b9af5 to your computer and use it in GitHub Desktop.
AnimationCurve PlayEvaluate
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