using UnityEngine;
namespace HeavenStudio.Util
{
public static class AnimationHelpers
{
public static bool IsAnimationNotPlaying(this Animator anim)
{
var stateInfo = anim.GetCurrentAnimatorStateInfo(0);
return (stateInfo.normalizedTime >= stateInfo.speed || stateInfo.loop) && !anim.IsInTransition(0);
}
///
/// Returns true if animName is currently playing on animator
///
/// Animator to check
/// name of animation to look out for
public static bool IsPlayingAnimationName(this Animator anim, string animName)
{
var stateInfo = anim.GetCurrentAnimatorStateInfo(0);
return (stateInfo.normalizedTime < stateInfo.speed || stateInfo.loop) && stateInfo.IsName(animName);
}
///
/// Sets animator's progress on an animation based on current song beat between startTime and length
/// function must be called in actor's Update loop to update properly
///
/// Animator to update
/// name of animation to play
/// reference start time of animation (progress 0.0)
/// duration of animation (progress 1.0)
/// multiplier for animation progress (smaller values make animation slower)
/// animator layer to play animation on
public static void DoScaledAnimation(this Animator anim, string animName, double startTime, float length = 1f, float timeScale = 1f, int animLayer = -1)
{
float pos = Conductor.instance.GetPositionFromBeat(startTime, length) * timeScale;
anim.Play(animName, animLayer, pos);
anim.speed = 1f; //not 0 so these can still play their script events
}
///
/// Sets animator progress on an animation according to pos
///
/// Animator to update
/// name of animation to play
/// position to set animation progress to (0.0 - 1.0)
/// animator layer to play animation on
public static void DoNormalizedAnimation(this Animator anim, string animName, float pos = 0f, int animLayer = -1)
{
anim.Play(animName, animLayer, pos);
anim.speed = 1f;
}
///
/// Plays animation on animator, scaling speed to song BPM
/// call this funtion once, when playing an animation
///
/// Animator to play animation on
/// name of animation to play
/// multiplier for animation speed
/// starting progress of animation
/// animator layer to play animation on
public static void DoScaledAnimationAsync(this Animator anim, string animName, float timeScale = 1f, float startPos = 0f, int animLayer = -1)
{
anim.Play(animName, animLayer, startPos);
anim.speed = (1f / Conductor.instance.pitchedSecPerBeat) * timeScale;
}
///
/// Plays animation on animator, at default speed
/// this is the least nessecary function here lol
///
/// Animator to play animation on
/// name of animation to play
/// starting progress of animation
/// animator layer to play animation on
public static void DoUnscaledAnimation(this Animator anim, string animName, float startPos = 0f, int animLayer = -1)
{
anim.Play(animName, animLayer, startPos);
anim.speed = 1f;
}
}
}