Best approach to launch animation from toggle switch?

I have been now playing around with the new UI system and got stuck on a rather simple problem.

I have my buttons set up with nice animations on the buttons. But I would like to now have my button (Toggle to be more precise) to play animation A when enabled and play animation B when disabled.

What would be the most convenient way to implement that? I tried to use the event system and also the OnValueChanged call but without success.

  • Cromfel

The way I made this work is by using the Animator.
I had a button that when it was pressed once, it would slide a panel in, and when pressed again the panel would slide out.
I set up both animations to react to triggers( “SlideIn” and “SlideOut” )
Then, when I press the button, I change the state of a boolean variable to true or false and depending on that value, I use animator.SetTrigger(“SlideIn”) (or SlideOut). And it works.

I don’t know if this is the best approach, but it worked for me and i stuck with it.

Thanks a lot for such swift reply BinaryX. Unfortunately I was not able to reproduce your technique. Apologies for my ignorance.

Here is my primitive snippet of code. How can I now play 2 separate animations in the enabled and disabled states? Everything else works just fine except that I can not play the animation.

I suppose Im doing something wrong on the animation side. I just took an object, went to animation windows and recorded keyframes for it to change oriantation. Now I want to play that animation. I have never used meccanim animator thingie so Im little bit illiterate on that side. I was hoping to just use animation.Play(); but apparently Im doing something totally wrong.

public class ToggleStateClass : MonoBehaviour {
public bool toggleState;
public void toggleStateFunction()
    {
        if (toggleState)
        {
            Debug.Log("Toggle disabled!");
            // animator.SetTrigger("myToggleDisabledAnimation")
            animation.Play(myToggleDisabledAnimation);
            toggleState=false;
        }
        else
        {
            Debug.Log("Toggle enabled!");
            // animator.SetTrigger("myToggleEnabledAnimation")
            animation.Play(myToggleEnabledAnimation);
            toggleState=true;
        }
    }
}

I was able to solve thew problem. It was indeed just Animations in wrong place and this legacy problem to set the Animation first to “Debug” from inspector and set the Animation Type to 1 (Supposedly it means legacy). Then it works…

Attached example project with current solution.

BinaryX could you elaborate on your approach?

Error:

I received this error but what i realized is that Unity automatically generated the mecanim file “Capsule” and there was remnants of the non-legacy animations. Opening the animator editor for capsule and simply deleting the old references removed the error also.

1829508–117256–ToggleButtonDrivingAnimation.zip (175 KB)

I’m glad you were able to fix your problem. I was just about to test your way of handling legacy animations to see how can i help you better.

So, let’s explain what I did here.

I put the Animator component on a button, created an Animation Controller and attached it to the button.
Don’t mind the Transitions from the Button component, that was just to test how those are working.

I added a function to the Button component that requires an Animator parameter. This is the function called when pressing the button :

public void MainButtonClicked(Animator currAnimator)
    {
        currAnimator.SetBool("Activated", !currAnimator.GetBool("Activated"));
    }

The Animator has a Parameter of type Bool (bottom left corner)

After you add the controller to the Animator component, when you create an animation from the Animation window (like you did before), it’s gonna show up in the Animator as a state, not connected to anything. The orange state is the default one.

There are 3 states in which this button can be : Normal, Activated, Activated Rev ( it’s another animation, but i used the Activated animation with speed -1 to reverse its effect )

From Normal to Activated, the Condition is for Activated to be True;
From Activated to Activated Rev, the Condition is for Activated to be False
From Activated Rev to Normal, the Condition is left default (Exit time)

Make sure the animations are not in a loop (Click the animation in the project, then in the Inspector uncheck Loop property).

That should do the job. When you first press the button, the Activated parameter will become true, it will go into Activated state (will play that animation and will stay there). When you press the button again, it will go into Activated Rev state (play the other animation). After then 2nd animation is complete, it will go back to Normal state.

k is there a simple way to just set up a triger button , on a cube that when you click the cube or object that the animations for another object turns on and off when you click the same cube, im terible at scripting , but i know how to make animations, any clue of a simple way or a script made or c scripts that i can download and drag into unity and then put on the thing i need it for