Need help getting the time of animation from the Animator

Hi there, I’m working on my first game and I just started finally adding a little more polish to the UI. I’m making UI animations like windows moving and fading, but I’ve run itno an issue:

I have a window that plays an animation when it closes, but while it’s open, the game is paused, and I want the game to unpause after the animation is done. The issue is I can’t seem to get the exact length of the animation clip that plays when the Animator goes into its close state.

Here is the coroutine that handles this:

public IEnumerator EndDialogueCoroutine()
    {
        animator.SetBool("IsOpen", false);
       
       
        yield return new WaitForSeconds(closeSpeed);
        uiManager.CloseAndResume();
    }

animator is the attached Animator component that has the animation states:

So right now in the code I am setting my own closeSpeed to a rough estimate, but what I want is to find out the length of the animation that plays when I change the bool to IsOpen = false.

Is there any way to do this ?

If you yield return null after setting the bool, you can use animator.GetNextAnimatorStateInfo(0).length (or GetCurrent… if the transition is instant).

Or if you would prefer to use a more sensible system you should check out Animancer (link in my signature) which would let you write something along the lines of animancer.Play(dialogueBoxOpen).Events.OnEnd = uiManager.CloseAndResume; (where dialogueBoxOpen is an AnimationClip). That way you don’t need to waste time messing around with Animator Controllers or even bother making a coroutine just to wait for the animation to end.

1 Like

@Kybernetik

I had been experimenting with those methods and you’re right that they are working! Thanks a lot, I’m new to the animating and I was getting confused by the layerIndex parameters going in those methods.