Author Topic: How do I use sprites to animate a button after it was clicked?  (Read 6056 times)

alexh0101

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 30
    • View Profile
After I click a button, I want it to animate with sprites.

The problem is if I use NGUI's button, I can't use Unity's Sprite Renderer which is necessary to use Unity's animation feature.

NGUI seems to have several features linked to sprite animation: "UI2D Sprite Animation", "Sprite Animation", "Play animation".

But what am I supposed to use?

Thanks in advance

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How do I use sprites to animate a button after it was clicked?
« Reply #1 on: July 18, 2014, 05:39:39 AM »
2D sprite animation -- it's an example script that shows you how to do it. You choose the frames of the animation, and it will do the rest.

alexh0101

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: How do I use sprites to animate a button after it was clicked?
« Reply #2 on: July 21, 2014, 08:50:59 AM »
Hi there, thanks for your response.

When you say it's an example script, are you saying that I should do some kind of copy/paste and incorporate it in my own script?

What I want to achieve is to animate the button (replace the button's sprite with an animation of 5 sprites), when the button is clicked. And when this 5-sprite-animation is over, I want a new function to be called.

Is that doable with NGUI?
Note: I'm using NGUI's UISprite component, not Unity's Sprite Renderer.

Thanks a lot

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How do I use sprites to animate a button after it was clicked?
« Reply #3 on: July 21, 2014, 05:32:58 PM »
For something like that I'd write a simple script.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MyAnimator : MonoBehaviour
  5. {
  6.     public UnityEngine.Sprite[] sprites;
  7.  
  8.     void OnClick ()
  9.     {
  10.         StartCoroutine(MyAnimation());
  11.     }
  12.  
  13.     IEnumerator MyAnimation()
  14.     {
  15.         UI2DSprite sp = GetComponent<UI2DSprite>();
  16.  
  17.         foreach (UnityEngine.Sprite u2 in sprites)
  18.         {
  19.             sp.sprite2D = u2;
  20.             yield return new WaitForSeconds(0.1f);
  21.         }
  22.         // TODO: Call your function here
  23.     }
  24. }
Nice, clean, and straight to the point. Replace "0.1f" with whatever delay you want it to be.