-
Notifications
You must be signed in to change notification settings - Fork 35
/
Tweener.cs
292 lines (239 loc) · 8.6 KB
/
Tweener.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.EventSystems;
/// <summary>
/// Base of tween.
/// </summary>
namespace uTools
{
public abstract class Tweener : MonoBehaviour
{
static public Tweener current;
public enum Style
{
Once,
Loop,
PingPong,
}
public EaseType method = EaseType.linear;
public Style style = Style.Once;
public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
public bool ignoreTimeScale = true;
public float delay = 0f;
public float duration = 1f;
[HideInInspector]
public bool steeperCurves = false;
public int tweenGroup = 0;
public UnityEvent onFinished = null;
public UnityAction onUpdate = null;
[HideInInspector]
public GameObject eventReceiver;
[HideInInspector]
public string callWhenFinished;
bool mStarted = false;
float mStartTime = 0f;
float mDuration = 0f;
float mAmountPerDelta = 1000f;
float mFactor = 0f;
/// <summary>
/// Amount advanced per delta time.
/// </summary>
public float amountPerDelta
{
get
{
if (mDuration != duration)
{
mDuration = duration;
mAmountPerDelta = Mathf.Abs((duration > 0f) ? 1f / duration : 1000f) * Mathf.Sign(mAmountPerDelta);
}
return mAmountPerDelta;
}
}
public float tweenFactor { get { return mFactor; } set { mFactor = Mathf.Clamp01(value); } }
public Direction direction { get { return amountPerDelta < 0f ? Direction.Reverse : Direction.Forward; } }
void Reset()
{
if (!mStarted)
{
SetStartToCurrentValue();
SetEndToCurrentValue();
}
}
protected virtual void Start() { Update(); }
void Update()
{
float delta = ignoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
if (!mStarted)
{
mStarted = true;
mStartTime = time + delay;
}
if (time < mStartTime) return;
// Advance the sampling factor
mFactor += amountPerDelta * delta;
// Loop style simply resets the play factor after it exceeds 1.
if (style == Style.Loop)
{
if (mFactor > 1f)
{
mFactor -= Mathf.Floor(mFactor);
}
}
else if (style == Style.PingPong)
{
// Ping-pong style reverses the direction
if (mFactor > 1f)
{
mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
mAmountPerDelta = -mAmountPerDelta;
}
else if (mFactor < 0f)
{
mFactor = -mFactor;
mFactor -= Mathf.Floor(mFactor);
mAmountPerDelta = -mAmountPerDelta;
}
}
// If the factor goes out of range and this is a one-time tweening operation, disable the script
if ((style == Style.Once) && (duration == 0f || mFactor > 1f || mFactor < 0f))
{
mFactor = Mathf.Clamp01(mFactor);
Sample(mFactor, true);
enabled = false;
if (current != this)
{
Tweener before = current;
current = this;
if (onFinished != null)
{
onFinished.Invoke();
}
// Deprecated legacy functionality support
if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
current = before;
}
}
else Sample(mFactor, false);
}
public void SetOnFinished(UnityEvent finishedCallBack) { onFinished = finishedCallBack; }
public void AddOnFinished(UnityEvent finishedCallBack) { }
public void RemoveOnFinished(UnityEvent finishedCallBack)
{
}
void OnDisable() { mStarted = false; }
/// <summary>
/// Sample the tween at the specified factor.
/// </summary>
public void Sample(float factor, bool isFinished)
{
// Calculate the sampling value
float val = Mathf.Clamp01(factor);
val = (method == EaseType.none) ? animationCurve.Evaluate(val) : EaseManager.EasingFromType(0, 1, val, method);
// Call the virtual update
OnUpdate((method == EaseType.none) ? animationCurve.Evaluate(val) : val, isFinished);
if (onUpdate != null)
{
onUpdate.Invoke();
}
}
/// <summary>
/// Play the tween forward.
/// </summary>
public void PlayForward() { Play(true); }
/// <summary>
/// Play the tween in reverse.
/// </summary>
public void PlayReverse() { Play(false); }
/// <summary>
/// Manually activate the tweening process, reversing it if necessary.
/// </summary>
public void Play(bool forward)
{
mAmountPerDelta = Mathf.Abs(amountPerDelta);
if (!forward) mAmountPerDelta = -mAmountPerDelta;
enabled = true;
Update();
}
/// <summary>
/// Manually reset the tweener's state to the beginning.
/// If the tween is playing forward, this means the tween's start.
/// If the tween is playing in reverse, this means the tween's end.
/// </summary>
public void ResetToBeginning()
{
mStarted = false;
mFactor = (amountPerDelta < 0f) ? 1f : 0f;
Sample(mFactor, false);
}
/// <summary>
/// Manually start the tweening process, reversing its direction.
/// </summary>
public void Toggle()
{
if (mFactor > 0f)
{
mAmountPerDelta = -amountPerDelta;
}
else
{
mAmountPerDelta = Mathf.Abs(amountPerDelta);
}
enabled = true;
}
/// <summary>
/// Actual tweening logic should go here.
/// </summary>
abstract protected void OnUpdate(float factor, bool isFinished);
/// <summary>
/// Starts the tweening operation.
/// </summary>
static public T Begin<T>(GameObject go, float duration) where T : Tweener
{
T comp = go.GetComponent<T>();
#if UNITY_FLASH
if ((object)comp == null) comp = (T)go.AddComponent<T>();
#else
// Find the tween with an unset group ID (group ID of 0).
if (comp != null && comp.tweenGroup != 0)
{
comp = null;
T[] comps = go.GetComponents<T>();
for (int i = 0, imax = comps.Length; i < imax; ++i)
{
comp = comps[i];
if (comp != null && comp.tweenGroup == 0) break;
comp = null;
}
}
if (comp == null)
{
comp = go.AddComponent<T>();
if (comp == null)
{
Debug.LogError("Unable to add " + typeof(T) + " to " + go);
return null;
}
}
#endif
comp.mStarted = false;
comp.duration = duration;
comp.mFactor = 0f;
comp.mAmountPerDelta = Mathf.Abs(comp.amountPerDelta);
comp.style = Style.Once;
comp.animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
comp.eventReceiver = null;
comp.callWhenFinished = null;
comp.enabled = true;
return comp;
}
public virtual void SetStartToCurrentValue() { }
public virtual void SetEndToCurrentValue() { }
public virtual void SetCurrentValueToStart() { }
public virtual void SetCurrentValueToEnd() { }
}
}