Search Unity

Question Need help getting the time of animation from the Animator

Discussion in 'Animation' started by PaperMouseGames, May 28, 2020.

  1. PaperMouseGames


    Jul 31, 2018
    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:

    Code (CSharp):
    1. public IEnumerator EndDialogueCoroutine()
    2.     {
    3.         animator.SetBool("IsOpen", false);
    6.         yield return new WaitForSeconds(closeSpeed);
    7.         uiManager.CloseAndResume();
    8.     }
    animator is the attached Animator component that has the animation states:

    UI animation 1.png

    So right now in the code I am setting my own
    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 ?
  2. Kybernetik


    Jan 3, 2013
    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;
    is an
    ). 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.
    PaperMouseGames likes this.
  3. PaperMouseGames


    Jul 31, 2018

    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.