Guest User

UI_ButtonScale.cs

a guest
Apr 8th, 2017
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5. [RequireComponent (typeof (Button))]
  6. public class UI_ButtonScale : MonoBehaviour,IPointerDownHandler ,IPointerUpHandler {
  7.     public AnimationCurve animCurve;
  8.     [Tooltip("Animation speed multiplier")]
  9.     public float speed = 1;
  10.     private Vector3 initScale;
  11.     public Transform target;
  12.  
  13.     Button button;
  14.     public Button Button
  15.     {
  16.         get
  17.         {
  18.             if (button == null)
  19.                 button = GetComponent <Button> ();
  20.            
  21.             return button;
  22.         }
  23.     }
  24.     // Use this for initialization
  25.     void Awake()
  26.     {
  27.         if(target == null)
  28.         target = transform;
  29.  
  30.         initScale = target.localScale;
  31.     }
  32.     void OnEnable()
  33.     {
  34.         target.localScale = initScale;
  35.     }
  36.     public void OnPointerDown(PointerEventData eventData)
  37.     {
  38.         if (Button != null && !Button.interactable)
  39.             return;
  40.        
  41.         StopCoroutine("ScaleIN");
  42.         StartCoroutine("ScaleIN");
  43.     }
  44.     public void OnPointerUp(PointerEventData eventData)
  45.     {
  46.         if (Button != null && !Button.interactable)
  47.             return;
  48.        
  49.         StopCoroutine("ScaleOut");
  50.         StartCoroutine("ScaleOut");
  51.     }
  52.  
  53.     IEnumerator ScaleIN()
  54.     {
  55.         target.localScale = initScale;
  56.         float t=0;
  57.         float maxT = animCurve.keys[animCurve.length-1].time;
  58.        
  59.         while(t<maxT)
  60.         {  
  61.             t+= speed * Time.unscaledDeltaTime;
  62.             target.localScale = Vector3.one * animCurve.Evaluate(t);
  63.             yield return null;
  64.         }
  65.     }
  66.     IEnumerator ScaleOut()
  67.     {
  68.         target.localScale = initScale;
  69.         float t=0;
  70.         float maxT = animCurve.keys[animCurve.length-1].time;
  71.        
  72.         while(t<maxT)
  73.         {  
  74.             t+= speed * Time.unscaledDeltaTime;
  75.             target.localScale = Vector3.one * animCurve.Evaluate(maxT-t);
  76.             yield return null;
  77.         }
  78.         transform.localScale = initScale;
  79.     }
  80. }
Add Comment
Please, Sign In to add comment