Author Topic: Animated Sprite Buttons  (Read 3777 times)

Cosmas

  • Guest
Animated Sprite Buttons
« on: May 29, 2012, 11:15:02 PM »
I'm sure this has been covered but I can't seem to find anything on it, maybe I'm using the wrong terms?

So I would like to have a sprite button with different animated sprite effects for each condition: Normal, Hover, Disabled, Active.
How would one go about doing this? Is it currently possible? If not, is it a planned feature?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Animated Sprite Buttons
« Reply #1 on: May 30, 2012, 12:27:28 AM »
You can create your own custom button using one or more sprite with UISpriteAnimation on them. Slap a collider on top, and it's a button. You'll need to add your own logic as to what will animate when though -- by simply adding an OnHover function and doing something inside.

Cosmas

  • Guest
Re: Animated Sprite Buttons
« Reply #2 on: May 30, 2012, 08:33:54 PM »
Many thanks ArenMook,
Here's my basic implementation if anyone is interested. Not the best way obviously, but given that the UISpriteAnimation List<> isn't accessible externally its one quick solution:

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4.         public class UIAnimatedButton : MonoBehaviour
  5.         {
  6.         public UIAtlas normalSprite;
  7.         public UIAtlas hoverSprite;
  8.         public UIAtlas pressedSprite;
  9.  
  10.         private UISprite buttonObject;
  11.         private UISpriteAnimation anime;
  12.  
  13.         public void Awake()
  14.         {
  15.             buttonObject = gameObject.GetComponent<UISprite>();
  16.             anime = gameObject.GetComponent<UISpriteAnimation>();
  17.         }
  18.  
  19.         public void OnMouseEnter()
  20.         {
  21.             Destroy(anime);
  22.             buttonObject.atlas = hoverSprite;
  23.             gameObject.AddComponent<UISpriteAnimation>();
  24.         }
  25.  
  26.         public void OnMouseExit()
  27.         {
  28.             Destroy(anime);
  29.             buttonObject.atlas = normalSprite;
  30.             gameObject.AddComponent<UISpriteAnimation>();
  31.         }
  32.  
  33.         public void OnMouseDown()
  34.         {
  35.             Destroy(anime);
  36.             buttonObject.atlas = pressedSprite;
  37.             gameObject.AddComponent<UISpriteAnimation>();
  38.         }
  39.  
  40. }
« Last Edit: May 30, 2012, 08:46:40 PM by Cosmas »