-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathAnimatedTextures.cs
More file actions
57 lines (44 loc) · 1.37 KB
/
AnimatedTextures.cs
File metadata and controls
57 lines (44 loc) · 1.37 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
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class AnimatedTextures : MonoBehaviour
{
public string Filename;
private List<Texture2D> mFrames = new List<Texture2D>();
private List<float> mFrameDelay = new List<float>();
private int mCurFrame = 0;
private float mTime = 0.0f;
void Start()
{
if( string.IsNullOrWhiteSpace( Filename ) )
{
return;
}
var path = Path.Combine( Application.streamingAssetsPath, Filename );
using( var decoder = new MG.GIF.Decoder( File.ReadAllBytes( path ) ) )
{
var img = decoder.NextImage();
while( img != null )
{
mFrames.Add( img.CreateTexture() );
mFrameDelay.Add( img.Delay / 1000.0f );
img = decoder.NextImage();
}
}
GetComponent<Renderer>().material.mainTexture = mFrames[0];
}
void Update()
{
if( mFrames == null )
{
return;
}
mTime += Time.deltaTime;
if( mTime >= mFrameDelay[ mCurFrame ] )
{
mCurFrame = ( mCurFrame + 1 ) % mFrames.Count;
mTime = 0.0f;
GetComponent<Renderer>().material.mainTexture = mFrames[mCurFrame];
}
}
}