-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI2DSpriteAnimation.cs
More file actions
55 lines (47 loc) · 1.35 KB
/
UI2DSpriteAnimation.cs
File metadata and controls
55 lines (47 loc) · 1.35 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
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//----------------------------------------------
#if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
using UnityEngine;
/// <summary>
/// Small script that makes it easy to create looping 2D sprite animations.
/// </summary>
public class UI2DSpriteAnimation : MonoBehaviour
{
public int framerate = 20;
public bool ignoreTimeScale = true;
public UnityEngine.Sprite[] frames;
UnityEngine.SpriteRenderer mUnitySprite;
UI2DSprite mNguiSprite;
int mIndex = 0;
float mUpdate = 0f;
void Start ()
{
mUnitySprite = GetComponent<UnityEngine.SpriteRenderer>();
mNguiSprite = GetComponent<UI2DSprite>();
if (framerate > 0) mUpdate = (ignoreTimeScale ? RealTime.time : Time.time) + 1f / framerate;
}
void Update ()
{
if (framerate != 0 && frames != null && frames.Length > 0)
{
float time = ignoreTimeScale ? RealTime.time : Time.time;
if (mUpdate < time)
{
mUpdate = time;
mIndex = NGUIMath.RepeatIndex(framerate > 0 ? mIndex + 1 : mIndex - 1, frames.Length);
mUpdate = time + Mathf.Abs(1f / framerate);
if (mUnitySprite != null)
{
mUnitySprite.sprite = frames[mIndex];
}
else if (mNguiSprite != null)
{
mNguiSprite.nextSprite = frames[mIndex];
}
}
}
}
}
#endif