-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUI2DSpriteAnimation.cs
121 lines (105 loc) · 2.08 KB
/
UI2DSpriteAnimation.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
using UnityEngine;
public class UI2DSpriteAnimation : MonoBehaviour
{
[SerializeField]
protected int framerate = 20;
public bool ignoreTimeScale = true;
public bool loop = true;
public Sprite[] frames;
private SpriteRenderer mUnitySprite;
private UI2DSprite mNguiSprite;
private int mIndex;
private float mUpdate;
public bool isPlaying => base.enabled;
public int framesPerSecond
{
get
{
return framerate;
}
set
{
framerate = value;
}
}
public void Play()
{
if (frames != null && frames.Length > 0)
{
if (!base.enabled && !loop)
{
int num = (framerate <= 0) ? (mIndex - 1) : (mIndex + 1);
if (num < 0 || num >= frames.Length)
{
mIndex = ((framerate < 0) ? (frames.Length - 1) : 0);
}
}
base.enabled = true;
UpdateSprite();
}
}
public void Pause()
{
base.enabled = false;
}
public void ResetToBeginning()
{
mIndex = ((framerate < 0) ? (frames.Length - 1) : 0);
UpdateSprite();
}
private void Start()
{
Play();
}
private void Update()
{
if (frames == null || frames.Length == 0)
{
base.enabled = false;
}
else if (framerate != 0)
{
float num = (!ignoreTimeScale) ? Time.time : RealTime.time;
if (mUpdate < num)
{
mUpdate = num;
int num2 = (framerate <= 0) ? (mIndex - 1) : (mIndex + 1);
if (!loop && (num2 < 0 || num2 >= frames.Length))
{
base.enabled = false;
}
else
{
mIndex = NGUIMath.RepeatIndex(num2, frames.Length);
UpdateSprite();
}
}
}
}
private void UpdateSprite()
{
if (mUnitySprite == null && mNguiSprite == null)
{
mUnitySprite = GetComponent<SpriteRenderer>();
mNguiSprite = GetComponent<UI2DSprite>();
if (mUnitySprite == null && mNguiSprite == null)
{
base.enabled = false;
return;
}
}
float num = (!ignoreTimeScale) ? Time.time : RealTime.time;
if (framerate != 0)
{
mUpdate = num + Mathf.Abs(1f / (float)framerate);
}
if (mUnitySprite != null)
{
mUnitySprite.sprite = frames[mIndex];
}
else if (mNguiSprite != null)
{
mNguiSprite.nextSprite = frames[mIndex];
}
}
}