Search Unity

Help Wanted Make transition duration depend on one clip's duration

Discussion in 'Animation' started by iKebab897, Oct 18, 2021.

  1. iKebab897

    iKebab897

    Joined:
    Aug 4, 2021
    Posts:
    31
    I'm making my first videogame. I'm creating my CustomButton class derived from UI.Button. It works entirely on an animator I created, which looks like this at the moment:

    upload_2021-10-18_20-47-58.png

    I'm trying to make an animator that I can reuse with every button in the game, and for different styles of buttons I just make an animator override controller and change the animations themselves, but the logic behind the transitions and everything needs to always stay the same.

    Now, it works wonders. There's just a couple problems:

    1. The transition from Normal to Highlighted needs to adapt to the length of the Highlight animation. For example, I'm making a button that takes 5 frames to fade from black to wide background. So, the transition, in order to not look weird, must last exactly the duration of the Highlight clip, like here:

    upload_2021-10-18_20-50-34.png

    This is of course not just the case from the Normal -> Highlighted transitions, in fact, most have this problem.
    I tried with toggling off Fixed Duration, but apparently it just means that instead of lasting a fixed amount in seconds, it last a certain percentage of the first animation (as far as I understand, this part of the animation side of Unity I don't really understand much), which is not what I'm looking for.
    Having to change this by hand for every button style and having multiple controller types is absolutely unacceptable and I must find a better soluton.

    2. The Normal animation is just a single-keyframe looped clip, while the others aren't looped and have multiple keyframes so that I can control how long the fade lasts.

    But what if I wanted to make an idle animation for when the button is highlighted? Can I create a single clip that has some custom loop points, so that I don't have to do two different states, and so multiple Animator Controller's?

    3. Is even using a custom animator for buttons optimal? On a performance level, and design as well? The alternative is using scripts (which works fine, I already did it before, but I have to make a script for every single kind of animation, like fade or scale over time, which is lots of work and repetition), or the default Unity Button (which doesn't let me animate text and other stuff).

    Is the approach I'm using good?
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    244
    Hey,

    There might be a simpler solution, but I think a hybrid approach of using an Animator Controller and a script would work for this.

    Instead of having transitions in the Animator Controller, you could just have the animations, and play them through a script. This way, you could dynamically set the crossfade time (transition time) depending on which animations you're using.

    https://docs.unity3d.com/ScriptReference/Animator.CrossFade.html
    https://docs.unity3d.com/ScriptReference/Animator.CrossFadeInFixedTime.html

    I'll elaborate on the fixed time checkbox. As you said, fixed time is the transition duration in real time seconds. Unchecking it turns the transition into normalized time, which is a percentage of the animation duration; 0 being 0% and 1 being 100%.
    Animator.CrossFade()
    is in normalized time (the same as the fixed time checkbox being unchecked), and
    Animator.CrossFadeInFixedTime()
    is in seconds.


    ---

    So to continue, if you want the transition to always be the same length as the highlight clip, you could store the clip length as a variable before playing the clip, and then play the clip using
    Animator.CrossFadeInFixedTime()
    and insert the clip length variable as the crossfade duration.

    Here is a forum post that explains a way to get clip length: https://forum.unity.com/threads/how-to-find-animation-clip-length.465751/

    Hopefully this helps. Let me know if anything is unclear. Of course, this is just a suggestion, there might be easier solutions.
     
  3. iKebab897

    iKebab897

    Joined:
    Aug 4, 2021
    Posts:
    31
    Thank you so much. This solved everything. Thank you.
     
    Unrighteouss likes this.
unityunity