Unity makes GIF animation playback components (without System.Drawing)
renderings
Packaged component effect
Instructions
- Is Play On Start: Autoplay
- Path: GIF path
- Is Use Fixed Frame Rate: Whether to use a fixed frame rate
- Frame Rate: fixed frame rate
- Loop: Whether to play in a loop
Use fixed frame rate If it is checked, the delay setting of each frame of the gif file will be ignored, and the value given by FrameRate will be used. If not checked, the value set by the gif file will be read.
Component method:
- Play(bool bRewind=false): Play, bRewind indicates whether to start playing from the beginning.
- Pause(): Pause
- Stop(): stop
- Free(): stop and release resources
core code
private void LoadGif()
{
images = null;
if (!System.IO.File.Exists(Path))
return;
if (!Path.EndsWith(".gif", System.StringComparison.OrdinalIgnoreCase))
return;
byte[] fileContent = System.IO.File.ReadAllBytes(Path);
List<GifImages> list = new List<GifImages>();
using( Decoder gifImage = new Decoder( fileContent ))
{
var image = gifImage.NextImage();
while( image != null )
{
Texture2D tex = image.CreateTexture();
Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
sp.name = $"GIF_{
list.Count:000}";
float dy = image.Delay / 1000f;
list.Add(new GifImages()
{
sprite = sp,
delay = dy
});
image = gifImage.NextImage();
}
}
if (list.Count > 0)
images = list.ToArray();
}