Mecanim: How do I get current animation state, save it and then restore later using script

Hi all,

I’m diving into the world of Mecanim (Unity 5.3.5f1) and it’s driving me nuts. As per title, how should I get current animation state, save it and then restore later? It seems like a simple task but it’s not when it comes to Mecanim (there’s no easy way to even get the name of the currently playing animation state!).

I’ve tried something like below, saving the current animation’s hash value and then trying to play it back later, but the saved hash value doesn’t match up with any of my animations and throws a warning instead?

private int savedHash;
private int hashStateIdle = Animator.StringToHash("Idle");
private int hashStateWalk = Animator.StringToHash("Walk");

void Start()
{
   animator = GetComponent<Animator>();
}

void Save()
{
    savedHash = animator.GetCurrentAnimatorStateInfo(0).GetHashCode();
}

void Restore()
{
    if (animator.GetCurrentAnimatorStateInfo(0).GetHashCode() != savedHash)
        animator.CrossFade(savedHash, 0.2f);
}

Any ideas? Thanks!

This seems to be because of performance reasons. I’m not a code guy - but found several posts which have solutions to similar issues. Hope these help.
http://answers.unity3d.com/questions/351534/how-to-get-current-state-on-mecanim.html
http://answers.unity3d.com/questions/153785/retrieving-the-name-of-the-animation-currently-pla.html
http://answers.unity3d.com/questions/1013423/animation-state-get-current-frame.html
http://answers.unity3d.com/questions/407186/how-to-get-current-state-name-on-mecanim.html
http://forum.unity3d.com/threads/current-animator-state-name.331803/
http://answers.unity3d.com/questions/1125067/mecanim-why-cant-i-get-current-state-name.html
http://answers.unity3d.com/questions/854219/how-to-get-the-current-motion-on-blend-tree.html

2 Likes

Thanks theANMATOR2b, it appears there’s no straight forward way to do this. Cheers

1 Like

Sorry for the necro, but for anyone also looking into this in the future I thought I’d leave my solution here. Its probably not perfect, but I haven’t spotted a problem (yet)

struct State
{
    public Vector3 Position;
    public Quaternion Rotation;
    public AnimatorStateInfo AnimatorState;
    public AnimatorStateInfo AnimatorStateNext;
    public AnimatorTransitionInfo AnimatorTransition;
}
State m_ReplayState;

public void RestoreState()
{
    transform.position = m_ReplayState.Position;
    Visuals.transform.rotation = m_ReplayState.Rotation;
    Anim.Play( m_ReplayState.AnimatorState.shortNameHash, 0, m_ReplayState.AnimatorState.normalizedTime );
    Anim.Update( 0f );
    Anim.CrossFadeInFixedTime( m_ReplayState.AnimatorStateNext.shortNameHash, m_ReplayState.AnimatorTransition.duration, 0, 0f, m_ReplayState.AnimatorTransition.normalizedTime );
}
public void RecordState()
{
    m_ReplayState.Position = transform.position;
    m_ReplayState.Rotation = Visuals.transform.rotation;
    m_ReplayState.AnimatorState = Anim.GetCurrentAnimatorStateInfo( 0 );
    m_ReplayState.AnimatorStateNext = Anim.GetNextAnimatorStateInfo( 0 );
    m_ReplayState.AnimatorTransition = Anim.GetAnimatorTransitionInfo( 0 );
}
6 Likes