Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- [RequireComponent (typeof (Button))]
- public class UI_ButtonScale : MonoBehaviour,IPointerDownHandler ,IPointerUpHandler {
- public AnimationCurve animCurve;
- [Tooltip("Animation speed multiplier")]
- public float speed = 1;
- private Vector3 initScale;
- public Transform target;
- Button button;
- public Button Button
- {
- get
- {
- if (button == null)
- button = GetComponent <Button> ();
- return button;
- }
- }
- // Use this for initialization
- void Awake()
- {
- if(target == null)
- target = transform;
- initScale = target.localScale;
- }
- void OnEnable()
- {
- target.localScale = initScale;
- }
- public void OnPointerDown(PointerEventData eventData)
- {
- if (Button != null && !Button.interactable)
- return;
- StopCoroutine("ScaleIN");
- StartCoroutine("ScaleIN");
- }
- public void OnPointerUp(PointerEventData eventData)
- {
- if (Button != null && !Button.interactable)
- return;
- StopCoroutine("ScaleOut");
- StartCoroutine("ScaleOut");
- }
- IEnumerator ScaleIN()
- {
- target.localScale = initScale;
- float t=0;
- float maxT = animCurve.keys[animCurve.length-1].time;
- while(t<maxT)
- {
- t+= speed * Time.unscaledDeltaTime;
- target.localScale = Vector3.one * animCurve.Evaluate(t);
- yield return null;
- }
- }
- IEnumerator ScaleOut()
- {
- target.localScale = initScale;
- float t=0;
- float maxT = animCurve.keys[animCurve.length-1].time;
- while(t<maxT)
- {
- t+= speed * Time.unscaledDeltaTime;
- target.localScale = Vector3.one * animCurve.Evaluate(maxT-t);
- yield return null;
- }
- transform.localScale = initScale;
- }
- }
Add Comment
Please, Sign In to add comment