Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Unity UI [SOLVED] UI and Animator not playing nice together

Discussion in 'UGUI & TextMesh Pro' started by Yandalf, Jan 7, 2020.

  1. Yandalf


    Feb 11, 2014
    Hey everyone!

    I'm creating a UI popup which I animate into view but I'm running into strange problems.
    The setup is pretty simple, I created a UI hierarchy with a few buttons and text elements, and when this hierarchy is set to active the opening up animation will play.
    From there on the animation controller goes into an empty idle animation, which then links to either a transition or a close animation based on triggers.
    When close is triggered, the animation will play, and the hierarchy deactivates again.

    This all works well the first time I run it, but on subsequent runs things go wrong!
    First of all when the Animator enters into the idle state it actually repositions itself like I did in the first frame of the opening animation, as if the animation wrapped around for a frame.
    I've been fiddling with all the options like turning Write Defaults on and off, changing the transition timing, etc. Nothing seems to work.

    Second issue is that my buttons don't seem to get their colours properly reset.
    I'm managing their colours through animation (I do some scaling as well so I can't simply use the colour transition mode), and when I disable a button it simply goes full transparent.
    When the panel gets closed all the buttons get set to non-interactive, and on panel activate the necessary buttons go back to interactive. The animator window shows the states going correctly, and the colours update accordingly EXCEPT for the alpha. Likewise, I've experimented with write defaults on and off and such, to no avail.

    What the hell is going on here? I've done similar things before and never seen this highly inconsistent behaviour!
    Is this an (old?)bug? I'm currently working in Unity 2018.3.8f1
  2. Yandalf


    Feb 11, 2014
    Okay, after a bit of searching I found the reason for this issue.
    It turns out that when an animator gets disabled and re-enabled later, it loses all state INCLUDING the default property states, re-initializing this when the Animator gets enabled again.
    If your animated object is not in the exact same pose as it was the first time you activated it, the default values will be different.

    Luckily the fix is rather simple, though it might not be recommended if you're using a ton of Animators in your scene.
    Simply get your Animator as soon as possible and set Animator.keepAnimatorControllerStateOnDisable to true.
    As the name implies, this will save ALL State on your Animator, so if you're relying on your Animator being in its default states again on enable you will have to make sure you either send it back to this state or guide your state to exit on Disable.
    This will also ensure your Animator's memory usage won't go down when it's disabled, hence why it may not be advisable when there's many Animators in your scene (such as when pooling animated objects).
    Fbrzd likes this.