- Home /
Unity 4.6 UI Button Click Animation?
We have animation states for Normal, Highlighted, Pressed and Disabled. How to set animation when a button is clicked?
What I want is the same functionality as NGUI where we are able to -
Click the button.
Play the button animation.
The button animation gets finished.
After that we call some method in some script.
Thanks in advance.
Worthy, knowledge for me about Unity 4.6.
Thanks Unity Gurus :)
Hi @Subhajit ,
Did You find a solution to this ? I am using unity 5 and have the same issue
Hi, Unfortunately not, there is no straight easy path to this. You could use Salazar's solution or you could use the onClick event to trigger the animation.
Anyways, this is not an answer. Try converting this to a comment.
Answer by AyAMrau · Sep 15, 2014 at 02:08 PM
The button component has a Transition property, you can set that to Animation and use it to trigger states in an Animator state machine.
Thanks for replying. Yes, that's what I was talking about. We have transition for normal, highlighted, pressed and disabled. Not for click event. When I connect animation to the pressed event (but intend for click), the action in the onclick method executes before finishing the animation.
This is true, evet have to work after animation end. But unity forgot about it.
Thanks for the answer been searching for this info for a couple days this is perfect for me to get my hands dirty and mess up stuff
Answer by Salazar · Jan 21, 2015 at 09:58 PM
I dont know if its the right answer but,
After you finished your animation keyframing, you can set a function(event) on to last frame of your animation. There is a add event button in animation window. Use it to fire a function via script.
Hope this helps.
Answer by Ivandir · Nov 16, 2014 at 05:51 AM
I believe this is a bug with Unity 4.6 beta. I have spent the last couple of hours addressing it and the best I can do is delay the OnClick event by animation clip length.
It works but it's not 100% accurate as Animator will sometimes spill over to Normal/Highlighted state due to clip length possibly being different.
public void MainMenu(){
StartCoroutine(DelayLoadMainMenu());
}
IEnumerator DelayLoadMainMenu(){
AnimationInfo[] currClip = anim.GetNextAnimationClipState (0);
Debug.Log ("AnimatorLength: " + currClip[0].clip.length);
Debug.Log ("AnimatorName: " + currClip[0].clip.name);
yield return new WaitForSeconds(currClip[0].clip.length);
Application.LoadLevel(mainMenuSceneName);
}
All this much coding just for a simple button animation? C'mon... UGUI is supposed to be easy to use and feature-rich right? This feature is a must, and should be built right in if it isn't so already I$$anonymous$$HO. :)
Answer by NavidAdelpour · Feb 08, 2019 at 07:19 PM
hi @Subhajit-Nath.
you need to attach this script to your button
all it do is that it will make all listenter off and when the event is triggered by animation (after the animation finished) it will make them on and invoke the functions...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class ButtonController : MonoBehaviour {
private Button button;
public void Awake() {
button = GetComponent<Button>();
// disabling all the event listeners
SetListenersState(UnityEventCallState.Off);
}
private void OnButtonClick() {
AudioManager.self.Play("button");
// enabling the listeners again
SetListenersState(UnityEventCallState.EditorAndRuntime);
// calling all listeres
button.onClick.Invoke();
// disabling all the event listeners
SetListenersState(UnityEventCallState.Off);
}
private void SetListenersState(UnityEventCallState state) {
for(int i = 0; i < button.onClick.GetPersistentEventCount(); i++) {
button.onClick.SetPersistentListenerState(i, state);
}
}
}
Your answer
Follow this Question
Related Questions
How to change UI button animation speed (4.6 UI) 2 Answers
Animator has not been initialized?? 3 Answers
Unity UI Button not working 1 Answer
How to make a button animation in menu 1 Answer
AddListener not working c# 0 Answers