Tanoda
UI_TweenScale.cs
Go to the documentation of this file.
1
5
6
7using System.Collections;
8
10{
29 [AddComponentMenu("UI/Extensions/UI Tween Scale")]
30 public class UI_TweenScale : MonoBehaviour
31 {
32 //ANIMATION FOR X AND Y, OR X IF isUniform set to false
33 public AnimationCurve animCurve;
34 [Tooltip("Animation speed multiplier")]
35 public float speed = 1;
36
37 [Tooltip("If true animation will loop, for best effect set animation curve to loop on start and end point")]
38
39 public bool isLoop = false;
40 //IF TRUE ANIMATION STARTS AUTOMATICALLY
41 [Tooltip("If true animation will start automatically, otherwise you need to call Play() method to start the animation")]
42
43 public bool playAtAwake = false;
44
45 [Space(10)]
46 //if true both x and y axis will be using animCurve;
47 [Header("Non uniform scale")]
48 [Tooltip("If true component will scale by the same amount in X and Y axis, otherwise use animCurve for X scale and animCurveY for Y scale")]
49 public bool isUniform = true;
50 //IF isUniform set to false use this for Y axis
51 public AnimationCurve animCurveY;
52 private Vector3 initScale;
53 private Transform myTransform;
54
55 void Awake()
56 {
57 myTransform = GetComponent<Transform>();
58 initScale = myTransform.localScale;
59 if (playAtAwake)
60 {
61 Play();
62 }
63 }
64
65 public void Play()
66 {
67 StartCoroutine("Tween");
68 }
69
70 Vector3 newScale = Vector3.one;
71
72 IEnumerator Tween()
73 {
74 myTransform.localScale = initScale;
75 float t = 0;
76 float maxT = animCurve.keys[animCurve.length - 1].time;
77
78 while (t < maxT || isLoop)
79 {
80 t += speed * Time.deltaTime;
81
82 if (!isUniform)
83 {
84 newScale.x = 1 * animCurve.Evaluate(t);
85 newScale.y = 1 * animCurveY.Evaluate(t);
86
87 myTransform.localScale = newScale;
88 }
89 else
90 {
91 myTransform.localScale = Vector3.one * animCurve.Evaluate(t);
92 }
93
94 yield return null;
95 }
96 }
97
98 public void ResetTween()
99 {
100 StopCoroutine("Tween");
101 myTransform.localScale = initScale;
102 }
103 }
104}
Dynamic scaling of text or image (including button) based on curves
Credit Erdener Gonenc - @PixelEnvision.