Neo-DragonCP

Unity - AnimatedButtonScaleDownOnPress

Dec 12th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;// Required when using Event data.
  5.  
  6. public class AnimatedButtonScaleDownOnPress : MonoBehaviour, IPointerUpHandler,IPointerDownHandler,IPointerEnterHandler,IPointerExitHandler {
  7.  
  8.     [SerializeField]
  9.     Vector3 scaleDownSize = new Vector3 (0.9f,0.9f,1f); // The size when the button is pressed
  10.     private Vector3 defaultScale; // This is the original size of the button
  11.  
  12.     [Space]
  13.  
  14.     [SerializeField]
  15.     bool doColorTintOnPress = true; // default - tint the button on press
  16.  
  17.     [SerializeField]
  18.     Color colorTint = new Color (0.7f, 0.7f, 0.7f, 1); // slight grey
  19.     Color colorOriginal; // the original color of the button before pressed
  20.  
  21.     Image imageOfButton;
  22.     private bool pressedButton;
  23.  
  24.  
  25.     private bool movedPointerOffButtonAfterPress; // Keep track of button pressed but moved pointer away from button but held the pressed input
  26.  
  27.     // Use this for initialization
  28.     void Start () {
  29.        
  30.         // Get the image component
  31.         imageOfButton = GetComponent<Image> ();
  32.  
  33.         // Save the original button color
  34.         colorOriginal = imageOfButton.color;
  35.  
  36.         // Save the original tranform scale
  37.         defaultScale = transform.localScale;
  38.     }
  39.  
  40.     #region Pointer Events
  41.  
  42.     public void OnPointerDown(PointerEventData eventData)
  43.     {
  44.         PressAnimation ();
  45.     }
  46.  
  47.     public void OnPointerUp(PointerEventData eventData)
  48.     {
  49.         ResetAnimation ();
  50.  
  51.         // Button is no longer pressed - reset the bools
  52.         movedPointerOffButtonAfterPress = false;
  53.         pressedButton = false;
  54.     }
  55.  
  56.     public void OnPointerEnter(PointerEventData eventData)
  57.     {
  58.         // Check if pressed the button, moved the pointer off the button but held the input
  59.         // then moved back onto the button (redo the press animation)
  60.         if (pressedButton == true && movedPointerOffButtonAfterPress == true)
  61.         {
  62.             PressAnimation ();
  63.         }
  64.     }
  65.  
  66.     public void OnPointerExit(PointerEventData eventData)
  67.     {
  68.         ResetAnimation ();
  69.         movedPointerOffButtonAfterPress = true;
  70.     }
  71.  
  72.     #endregion
  73.  
  74.     #region Button Animations
  75.  
  76.     public void PressAnimation()
  77.     {
  78.         // Set the button as pressed;
  79.         pressedButton = true;
  80.  
  81.         // Change the color if needed
  82.         if (doColorTintOnPress == true){
  83.             imageOfButton.color = colorTint;
  84.         }
  85.  
  86.         // Update the scale
  87.         transform.localScale = scaleDownSize;
  88.     }
  89.  
  90.     public void ResetAnimation()
  91.     {
  92.         // Reset the scale
  93.         transform.localScale = defaultScale;
  94.  
  95.         // Reset the color
  96.         if (doColorTintOnPress == true){
  97.             imageOfButton.color = colorOriginal;
  98.         }
  99.  
  100.     }
  101.  
  102.     #endregion
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment