Last active
July 22, 2017 22:33
-
-
Save onionmk2/fb417d0e1c36440e2d2778bfe2016744 to your computer and use it in GitHub Desktop.
wait until the animation finishes the <see cref="m_normalizedTime "/> part
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 UnityEngine; | |
// ReSharper disable InconsistentNaming | |
///<summary> | |
/// wait until the animation finishes the <see cref="m_normalizedTime "/> part. | |
/// </summary> | |
/// <remarks> | |
/// original code is http://tsubakit1.hateblo.jp/entry/2016/02/11/021743. The credit goes to him. | |
/// </remarks> | |
/// <example> | |
/// how to use <see cref="WaitForSecondsRealtime"/> | |
/// | |
/// #1. animator.play | |
/// <code> | |
/// animator.Play(myState); | |
/// yield return null; // waiting 1frame required so that animator can know state change. | |
/// yield return new WaitAnimation(animator: animator, layerNo: 0, normalizedTime: 1f); // this is waiting the finish of (animation of (myState)) | |
/// </code> | |
/// | |
/// #2. animator.crossFadeInFixedTime | |
/// <code> | |
/// animator.CrossFadeInFixedTime(myState, transitionSecs); | |
/// yield return new WaitForSecondsRealtime(transitionSecs); // waiting transitionSecs required so that I wait that current state becomes myState. | |
/// yield return new WaitAnimation(animator: animator, layerNo: 0, normalizedTime: 1f); // this is waiting the finish of (animation of (myState)) | |
/// </code> | |
/// | |
/// </example> | |
public class WaitAnimation : CustomYieldInstruction | |
{ | |
private Animator m_animator; | |
private int m_lastStateHash; | |
private int m_layerNo; | |
private float m_normalizedTime; | |
public WaitAnimation(Animator animator, int layerNo, float normalizedTime = 1) | |
{ | |
m_layerNo = layerNo; | |
m_animator = animator; | |
m_lastStateHash = animator.GetCurrentAnimatorStateInfo(layerNo).fullPathHash; | |
m_normalizedTime = normalizedTime; | |
} | |
public override bool keepWaiting | |
{ | |
get | |
{ | |
var currentAnimatorState = m_animator.GetCurrentAnimatorStateInfo(m_layerNo); | |
return currentAnimatorState.fullPathHash == m_lastStateHash && | |
currentAnimatorState.normalizedTime < m_normalizedTime; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment