- Home /
how to make ui button run an animation
I'm trying to make a button on my main menu run an animation to move that menu off screen and move a new menu on the screen (like on my main menu if i hit my settings button it moves my main menu of screen and my settings menu on screen) i have all the animations all ready i just need a script to run it I'm not so good with script and could use some help. thanks.
Answer by Mmmpies · Feb 20, 2015 at 07:27 PM
OK this is a basic script that'll give you an idea of what to do. The two panels go into the public slots and you need a bool on each panels Animator called SwitchMenu. On one of the Menus play the exit Animation if true and on the other the enter Animation if true. Then do the opposite for the false settings for the bool. Make sure you switch off Loop for the animations.
Drag this script onto the canvas:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MenuSwitch : MonoBehaviour {
private Animator p1Anim;
private Animator p2Anim;
private bool swapPanel = false;
public RectTransform panel1;
public RectTransform panel2;
// Use this for initialization
void Start () {
p1Anim = panel1.GetComponent<Animator>();
p2Anim = panel2.GetComponent<Animator>();
}
public void IveBeenClicked()
{
swapPanel = !swapPanel;
p1Anim.SetBool("SwitchMenu", swapPanel);
p2Anim.SetBool("SwitchMenu", swapPanel);
}
}
On the button on each panel that swaps click + on the OnClick and drag the canvas onto the the slot that appears. From the dropdown select MenuSwitch -> IveBeenClicked.
Should be able to swap between two panels, I could in a quick test.
Example script in C#
thanks a lot i got every thing set up except I'm not sure where to put this in (like how to do this part)
On one of the $$anonymous$$enus play the exit Animation if true and on the other the enter Animation if true. Then do the opposite for the false settings for the bool.
O$$anonymous$$ you have to open the Animator Window for each menu and give it 2 states (actually you probably want more but lets just get this fixed first)
Have the animation to exit and the animation to enter. Right click on one and select $$anonymous$$ake Transition and click on the other button to make a transition. Do the same for the other button.
Click on the bottom left + to add a parameter and select bool. Name the bool Switch$$anonymous$$enu.
Click on the arrow and set the Condition to be Switch$$anonymous$$enu True or False and do the opposite for the other arrow.
Now open the other menu panel's Animator and copy what you just did but switch which arrow uses true and which uses false.
Your answer
Follow this Question
Related Questions
Animation script help light problem 0 Answers
is there a simple way to display a UI box around multiple animated meshs? 0 Answers
the animation walk but he still in the same place and rotate left and right on this white circle 0 Answers
First script plays animation but second script stops if from playing said animation 1 Answer
Animating a Panel,Animating a panel 1 Answer