-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUIValAnim.cs
72 lines (62 loc) · 1.49 KB
/
UIValAnim.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UIValueAnimator : MonoBehaviour {
float animateTime = 1.0f;
float finishTime = -1.0f;
AnimationCurve curve = null;
Text _element = null;
Text element {
get {
if (_element == null) {
_element = GetComponent<Text> ();
if (_element == null) {
Debug.LogError ("Could not find Text element on UIValueAnimator.");
}
}
return _element;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (finishTime > 0) {
if (Time.time < finishTime) {
SetValue(Mathf.FloorToInt (curve.Evaluate (Time.time)));
} else {
SetValue (Mathf.FloorToInt (curve.Evaluate (finishTime)));
curve = null;
finishTime = -1f;
}
}
}
int GetCurrentValue()
{
int result = 0;
int.TryParse (element.text, out result);
return result;
}
// Set the value of the text immediately without animating.
//
// value: new value for the element.
//
public void SetValue(int value)
{
element.text = "" + value;
}
// Animate toward a new value.
//
// endVal: new value to animate toward.
// animTime (optional): time to take getting to endVal. Omit to use value set in inspector.
//
public void AnimateValue(int endVal, float animTime = -1.0f)
{
if (animTime > 0) {
animateTime = animTime;
}
finishTime = Time.time + animateTime;
curve = AnimationCurve.EaseInOut (Time.time, GetCurrentValue(), finishTime, endVal);
}
}