-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveAnimation.cs
More file actions
386 lines (329 loc) · 9.57 KB
/
ActiveAnimation.cs
File metadata and controls
386 lines (329 loc) · 9.57 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//----------------------------------------------
#if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
#define USE_MECANIM
#endif
using UnityEngine;
using AnimationOrTween;
using System.Collections.Generic;
/// <summary>
/// Mainly an internal script used by UIButtonPlayAnimation, but can also be used to call
/// the specified function on the game object after it finishes animating.
/// </summary>
[AddComponentMenu("NGUI/Internal/Active Animation")]
public class ActiveAnimation : MonoBehaviour
{
/// <summary>
/// Active animation that resulted in the event notification.
/// </summary>
static public ActiveAnimation current;
/// <summary>
/// Event delegates called when the animation finishes.
/// </summary>
public List<EventDelegate> onFinished = new List<EventDelegate>();
// Deprecated functionality, kept for backwards compatibility
[HideInInspector] public GameObject eventReceiver;
[HideInInspector] public string callWhenFinished;
Animation mAnim;
Direction mLastDirection = Direction.Toggle;
Direction mDisableDirection = Direction.Toggle;
bool mNotify = false;
#if USE_MECANIM
Animator mAnimator;
string mClip = "";
float playbackTime
{
get
{
AnimatorStateInfo state = mAnimator.GetCurrentAnimatorStateInfo(0);
return Mathf.Clamp01(state.normalizedTime);
}
}
#endif
/// <summary>
/// Whether the animation is currently playing.
/// </summary>
public bool isPlaying
{
get
{
if (mAnim == null)
{
#if USE_MECANIM
if (mAnimator != null)
{
if (mLastDirection == Direction.Reverse)
{
if (playbackTime == 0f) return false;
}
else if (playbackTime == 1f) return false;
return true;
}
#endif
return false;
}
foreach (AnimationState state in mAnim)
{
if (!mAnim.IsPlaying(state.name)) continue;
if (mLastDirection == Direction.Forward)
{
if (state.time < state.length) return true;
}
else if (mLastDirection == Direction.Reverse)
{
if (state.time > 0f) return true;
}
else return true;
}
return false;
}
}
/// <summary>
/// Immediately finish playing the animation.
/// </summary>
public void Finish ()
{
if (mAnim != null)
{
foreach (AnimationState state in mAnim)
{
if (mLastDirection == Direction.Forward) state.time = state.length;
else if (mLastDirection == Direction.Reverse) state.time = 0f;
}
}
#if USE_MECANIM
else if (mAnimator != null)
{
mAnimator.Play(mClip, 0, (mLastDirection == Direction.Forward) ? 1f : 0f);
}
#endif
}
/// <summary>
/// Manually reset the active animation to the beginning.
/// </summary>
public void Reset ()
{
if (mAnim != null)
{
foreach (AnimationState state in mAnim)
{
if (mLastDirection == Direction.Reverse) state.time = state.length;
else if (mLastDirection == Direction.Forward) state.time = 0f;
}
}
#if USE_MECANIM
else if (mAnimator != null)
{
mAnimator.Play(mClip, 0, (mLastDirection == Direction.Reverse) ? 1f : 0f);
}
#endif
}
/// <summary>
/// Event receiver is only kept for backwards compatibility purposes. It's removed on start if new functionality is used.
/// </summary>
void Start ()
{
if (eventReceiver != null && EventDelegate.IsValid(onFinished))
{
eventReceiver = null;
callWhenFinished = null;
}
}
/// <summary>
/// Notify the target when the animation finishes playing.
/// </summary>
void Update ()
{
float delta = RealTime.deltaTime;
if (delta == 0f) return;
#if USE_MECANIM
if (mAnimator != null)
{
mAnimator.Update((mLastDirection == Direction.Reverse) ? -delta : delta);
if (isPlaying) return;
mAnimator.enabled = false;
enabled = false;
}
else if (mAnim != null)
#else
if (mAnim != null)
#endif
{
bool playing = false;
foreach (AnimationState state in mAnim)
{
if (!mAnim.IsPlaying(state.name)) continue;
float movement = state.speed * delta;
state.time += movement;
if (movement < 0f)
{
if (state.time > 0f) playing = true;
else state.time = 0f;
}
else
{
if (state.time < state.length) playing = true;
else state.time = state.length;
}
}
mAnim.Sample();
if (playing) return;
enabled = false;
}
else
{
enabled = false;
return;
}
if (mNotify)
{
mNotify = false;
if (current == null)
{
current = this;
EventDelegate.Execute(onFinished);
// Deprecated functionality, kept for backwards compatibility
if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
eventReceiver.SendMessage(callWhenFinished, SendMessageOptions.DontRequireReceiver);
current = null;
}
if (mDisableDirection != Direction.Toggle && mLastDirection == mDisableDirection)
NGUITools.SetActive(gameObject, false);
}
}
/// <summary>
/// Play the specified animation.
/// </summary>
void Play (string clipName, Direction playDirection)
{
// Determine the play direction
if (playDirection == Direction.Toggle)
playDirection = (mLastDirection != Direction.Forward) ? Direction.Forward : Direction.Reverse;
if (mAnim != null)
{
// We will sample the animation manually so that it works when the time is paused
enabled = true;
mAnim.enabled = false;
bool noName = string.IsNullOrEmpty(clipName);
// Play the animation if it's not playing already
if (noName)
{
if (!mAnim.isPlaying) mAnim.Play();
}
else if (!mAnim.IsPlaying(clipName))
{
mAnim.Play(clipName);
}
// Update the animation speed based on direction -- forward or back
foreach (AnimationState state in mAnim)
{
if (string.IsNullOrEmpty(clipName) || state.name == clipName)
{
float speed = Mathf.Abs(state.speed);
state.speed = speed * (int)playDirection;
// Automatically start the animation from the end if it's playing in reverse
if (playDirection == Direction.Reverse && state.time == 0f) state.time = state.length;
else if (playDirection == Direction.Forward && state.time == state.length) state.time = 0f;
}
}
// Remember the direction for disable checks in Update()
mLastDirection = playDirection;
mNotify = true;
mAnim.Sample();
}
#if USE_MECANIM
else if (mAnimator != null)
{
if (enabled && isPlaying)
{
if (mClip == clipName)
{
mLastDirection = playDirection;
return;
}
}
enabled = true;
mNotify = true;
mLastDirection = playDirection;
mClip = clipName;
mAnimator.Play(mClip, 0, (playDirection == Direction.Forward) ? 0f : 1f);
// NOTE: If you are getting a message "Animator.GotoState: State could not be found"
// it means that you chose a state name that doesn't exist in the Animator window.
}
#endif
}
/// <summary>
/// Play the specified animation on the specified object.
/// </summary>
static public ActiveAnimation Play (Animation anim, string clipName, Direction playDirection,
EnableCondition enableBeforePlay, DisableCondition disableCondition)
{
if (!NGUITools.GetActive(anim.gameObject))
{
// If the object is disabled, don't do anything
if (enableBeforePlay != EnableCondition.EnableThenPlay) return null;
// Enable the game object before animating it
NGUITools.SetActive(anim.gameObject, true);
// Refresh all panels right away so that there is no one frame delay
UIPanel[] panels = anim.gameObject.GetComponentsInChildren<UIPanel>();
for (int i = 0, imax = panels.Length; i < imax; ++i) panels[i].Refresh();
}
ActiveAnimation aa = anim.GetComponent<ActiveAnimation>();
if (aa == null) aa = anim.gameObject.AddComponent<ActiveAnimation>();
aa.mAnim = anim;
aa.mDisableDirection = (Direction)(int)disableCondition;
aa.onFinished.Clear();
aa.Play(clipName, playDirection);
if (aa.mAnim != null) aa.mAnim.Sample();
#if USE_MECANIM
else if (aa.mAnimator != null) aa.mAnimator.Update(0f);
#endif
return aa;
}
/// <summary>
/// Play the specified animation.
/// </summary>
static public ActiveAnimation Play (Animation anim, string clipName, Direction playDirection)
{
return Play(anim, clipName, playDirection, EnableCondition.DoNothing, DisableCondition.DoNotDisable);
}
/// <summary>
/// Play the specified animation.
/// </summary>
static public ActiveAnimation Play (Animation anim, Direction playDirection)
{
return Play(anim, null, playDirection, EnableCondition.DoNothing, DisableCondition.DoNotDisable);
}
#if USE_MECANIM
/// <summary>
/// Play the specified animation on the specified object.
/// </summary>
static public ActiveAnimation Play (Animator anim, string clipName, Direction playDirection,
EnableCondition enableBeforePlay, DisableCondition disableCondition)
{
if (!NGUITools.GetActive(anim.gameObject))
{
// If the object is disabled, don't do anything
if (enableBeforePlay != EnableCondition.EnableThenPlay) return null;
// Enable the game object before animating it
NGUITools.SetActive(anim.gameObject, true);
// Refresh all panels right away so that there is no one frame delay
UIPanel[] panels = anim.gameObject.GetComponentsInChildren<UIPanel>();
for (int i = 0, imax = panels.Length; i < imax; ++i) panels[i].Refresh();
}
ActiveAnimation aa = anim.GetComponent<ActiveAnimation>();
if (aa == null) aa = anim.gameObject.AddComponent<ActiveAnimation>();
aa.mAnimator = anim;
aa.mDisableDirection = (Direction)(int)disableCondition;
aa.onFinished.Clear();
aa.Play(clipName, playDirection);
if (aa.mAnim != null) aa.mAnim.Sample();
#if USE_MECANIM
else if (aa.mAnimator != null) aa.mAnimator.Update(0f);
#endif
return aa;
}
#endif
}