-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathUISpriteAnimation.cs
More file actions
78 lines (60 loc) · 1.67 KB
/
Copy pathUISpriteAnimation.cs
File metadata and controls
78 lines (60 loc) · 1.67 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
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System;
public class UISpriteAnimation
{
public bool loopReverse; // should we play the animation in reverse when we loop?
public float frameTime = 0.2f;
private List<UIUVRect> uvRects = new List<UIUVRect>();
private bool _isPlaying;
public bool isPlaying { get { return _isPlaying; } }
public UISpriteAnimation( float frameTime, List<UIUVRect> uvRects )
{
this.frameTime = frameTime;
this.uvRects = uvRects;
}
public IEnumerator play( UISprite sprite, int loopCount )
{
// store the original uvFrame so we can restore it when done
var originalUVFrame = sprite.uvFrame;
var totalFrames = uvRects.Count;
var currentFrame = 0;
var waiter = new WaitForSeconds( frameTime );
bool loopingForward = true;
_isPlaying = true;
// loop while we are playing and we havent finished looping
while( _isPlaying && (loopCount > 0 || loopCount==-1))
{
// what frame are we on?
if( loopingForward )
++currentFrame;
else
--currentFrame;
// bounds check
if( currentFrame < 0 || currentFrame == totalFrames )
{
// finished a loop, increment loop counter, reverse loop direction if necessary and reset currentFrame
if (loopCount>0)
{
--loopCount;
}
if( loopReverse )
loopingForward = !loopingForward;
if( loopingForward )
currentFrame = 0;
else
--currentFrame;
}
// set the new uvRect
sprite.uvFrame = uvRects[currentFrame];
yield return waiter;
}
// all done, restore the original frame
sprite.uvFrame = originalUVFrame;
}
public void stop()
{
_isPlaying = false;
}
}