- Home /
How To Play Animation On UI Button Click Please... Im Going To Freak Out Please Enter And Help Me
Its Have Been Days That I Looked For The Answers... I Just Want To Play An Simple Animation When Im Clicking My UI Button Iv Got An Script And Im Clicking My Button Its Playing The Animation... But When Im Clicking On It Agian And Nothing Happend. Please Help Me How To Figure It Out. Script:
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class play_anim_on_ui_button : MonoBehaviour {
public Button Text;
public Animator ani;
void Start ()
{
ani.enabled = false;
}
public void Press()
{
ani.enabled = true;
}
}
Answer by isablue27 · Jul 12, 2017 at 03:14 AM
Create a new c# script and paste this code. add the script to the object that own the animation
//Script Made By StrupsGames// using UnityEngine; using UnityEngine.UI; using System.Collections;
public class play_anim_on_ui_button : MonoBehaviour {
public Button Text;
public AudioClip sound;
public Animator ani;
public Canvas yourcanvas;
void Start ()
{
Text = Text.GetComponent<Button> ();
ani.enabled = false;
yourcanvas.enabled = true;
}
public void Press()
{
Text.enabled = true;
AudioSource.PlayClipAtPoint(sound, transform.position);
ani.enabled = true;
}
}
then select the object on the hierachy, go to the inspector, you are going to see this link text
fill it
then go to your UI button and in the inspector, on click() , add to list, and http://imgur.com/V4M3lSR
this function will enable our animator, so the fist time we click out button, the object is gonna be animated but the second time we click it, it wont, to make this happen, "add to list" other function and
once you did that, in the function choose :
now every time you click the button this function will play the animation
if you want to add sound to your running animation you just need to create an audio source and in the inspector add the audio clip http://imgur.com/xOtHf5V
then go to the button inspector and add a new function, "add to list" drag the object audio source you created and you will have to choose the audio clip again http://imgur.com/j4DkHnR
now just select audio source function, playoneshot(Audioclip)
This way every time you click it will reproduce the animation and the sound at the same time
i hope beeing helpful
You know man, detailed answer and all, but the reason I an Voting you up is because you replied to a question asked in 2015. THats some serious dedication.
Answer by allenallenallen · Dec 30, 2015 at 04:31 AM
All you did every time you press is enable the animator. That's it. When the animator is enabled, it plays the default animation once and only once.
Use this: http://docs.unity3d.com/ScriptReference/Animation.Play.html
Play the animation after enabling the animator each time you press the button.