Tanoda
SelectableScaler.cs
Go to the documentation of this file.
1
3
4using System.Collections;
6
8{
9 [AddComponentMenu("UI/Extensions/Selectable Scalar")]
10 [RequireComponent(typeof(Button))]
11 public class SelectableScaler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
12 {
13 public AnimationCurve animCurve;
14 [Tooltip("Animation speed multiplier")]
15 public float speed = 1;
16 private Vector3 initScale;
17 public Transform target;
18
19 Selectable selectable;
20 public Selectable Target
21 {
22 get
23 {
24 if (selectable == null)
25 selectable = GetComponent<Selectable>();
26
27 return selectable;
28 }
29 }
30 // Use this for initialization
31 void Awake()
32 {
33 if (target == null)
34 target = transform;
35
36 initScale = target.localScale;
37 }
38 void OnEnable()
39 {
40 target.localScale = initScale;
41 }
42 public void OnPointerDown(PointerEventData eventData)
43 {
44 if (Target != null && !Target.interactable)
45 return;
46
47 StopCoroutine("ScaleOUT");
48 StartCoroutine("ScaleIN");
49 }
50 public void OnPointerUp(PointerEventData eventData)
51 {
52 if (Target != null && !Target.interactable)
53 return;
54
55 StopCoroutine("ScaleIN");
56 StartCoroutine("ScaleOUT");
57 }
58
59 IEnumerator ScaleIN()
60 {
61 if (animCurve.keys.Length > 0)
62 {
63 target.localScale = initScale;
64 float t = 0;
65 float maxT = animCurve.keys[animCurve.length - 1].time;
66
67 while (t < maxT)
68 {
69 t += speed * Time.unscaledDeltaTime;
70 target.localScale = Vector3.one * animCurve.Evaluate(t);
71 yield return null;
72 }
73 }
74 }
75 IEnumerator ScaleOUT()
76 {
77 if (animCurve.keys.Length > 0)
78 {
79 //target.localScale = initScale;
80 float t = 0;
81 float maxT = animCurve.keys[animCurve.length - 1].time;
82
83 while (t < maxT)
84 {
85 t += speed * Time.unscaledDeltaTime;
86 target.localScale = Vector3.one * animCurve.Evaluate(maxT - t);
87 yield return null;
88 }
89 transform.localScale = initScale;
90 }
91 }
92 }
93}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
void OnPointerUp(PointerEventData eventData)
void OnPointerDown(PointerEventData eventData)
Credit Erdener Gonenc - @PixelEnvision.