Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Subhajit-Nath · Sep 15, 2014 at 01:33 PM · animationuibuttonclick

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image saud_ahmed020 · Apr 01, 2015 at 02:29 PM 0
Share

Worthy, knowledge for me about Unity 4.6.

Thanks Unity Gurus :)

avatar image MiraiTunga · Apr 16, 2016 at 10:10 AM 0
Share

Hi @Subhajit ,

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

avatar image Subhajit-Nath MiraiTunga · Apr 24, 2016 at 04:45 PM 1
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

The tutorial video explains how to set this up in editor

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Subhajit-Nath · Sep 15, 2014 at 05:54 PM 1
Share

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.

avatar image CodeFighter · Nov 11, 2014 at 11:17 AM 0
Share

This is true, evet have to work after animation end. But unity forgot about it.

avatar image bodec · Dec 30, 2014 at 06:03 AM 0
Share

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

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Subhajit-Nath · Nov 18, 2014 at 05:28 PM 2
Share

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. :)

avatar image
0

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);
         }
     }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges