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 -

  1. Click the button.
  2. Play the button animation.
  3. The button animation gets finished.
  4. After that we call some method in some script.

Thanks in advance.

The button component has a Transition property, you can set that to Animation and use it to trigger states in an Animator state machine.

The tutorial video explains how to set this up in editor

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.

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);
}

Hi @Subhajit ,

Did You find a solution to this ? I am using unity 5 and have the same issue

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);
        }
    }

}