Unity中Gif图片的播放

//====================================
//描 述 : Gif播放
//作 者 :  SHY
//创建时间 :2018/07/19 10:53:20  
//版 本 :  1.0
// ===============================================
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Drawing;//需要下载这个dll放到工程中
using System.Drawing.Imaging;
using System.IO;
using System;


/// <summary>
/// Gif --> Bitmap --> Stream --> Byte数组 --> Texture2D对象
/// </summary>
public class GifPlayer : MonoBehaviour {
    public System.Drawing.Image gifImage;                                 //Gif图片
    public UnityEngine.UI.Image image;                                    //显示Gif的UI

    private float fps = 24;
    private List<Texture2D> text2DList;                                  //Gif拆成的图片
    private float time;
    Bitmap myBitmap;


    private void Start(){
        gifImage = System.Drawing.Image.FromFile(Application.streamingAssetsPath + "/Sloar_System.gif");
        text2DList = GifToTexture2D(gifImage);
    }

    private void Update(){
        if (text2DList.Count > 0) {
            time += Time.deltaTime;
            int index = (int)(time * fps) % text2DList.Count;
            Debug.Log(index);
            if (image != null) {
                Sprite sprite = Sprite.Create(text2DList[index],
                    new Rect(0, 0, text2DList[index].width, text2DList[index].height),
                    new Vector2(0.5f, 0.5f));
                image.overrideSprite = sprite;
            }
        }
    }


    List<Texture2D> GifToTexture2D(System.Drawing.Image gifImage) {
        List<Texture2D> target = null;
        if (gifImage != null) {
            target = new List<Texture2D>();
            //图片的构成有两种形式:1.多页(.gif) 2.多分辨率
            Debug.Log("dimenson:" + gifImage.FrameDimensionsList.Length);
            //根据指定的GUID(gifImage.FrameDimensionsList[0])
            //创建一个提供获取图像框架维度信息的实例
            FrameDimension frameDimension = new FrameDimension(gifImage.FrameDimensionsList[0]);

            int frameCount = gifImage.GetFrameCount(frameDimension);
            for (int i = 0; i < frameCount; i++){
                gifImage.SelectActiveFrame(frameDimension, i);
                //创建Bitmap的实例
                Bitmap frameBitmap = new Bitmap(gifImage.Width, gifImage.Height);
                //Save
                //frameBitmap.Save(Application.dataPath + "/" + i + ".png", ImageFormat.Png);
                //将GifImage通过Graphics画到Bitmap上
                using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(frameBitmap)) {
                    graphics.DrawImage(gifImage, Point.Empty);
                }
                //创建Texture2D的实例
                Texture2D frameTexture2D = new Texture2D(frameBitmap.Width, frameBitmap.Height, TextureFormat.ARGB32, true);
                frameTexture2D.LoadImage(Bitmap2Byte(frameBitmap));
                target.Add(frameTexture2D);
            }
        }
        return target;
    }

    private byte[] Bitmap2Byte(Bitmap bitmap){
        using (MemoryStream stream = new MemoryStream()){
            // 将bitmap 以png格式保存到流中
            bitmap.Save(stream, ImageFormat.Png);
            // 创建一个字节数组,长度为流的长度
            byte[] data = new byte[stream.Length];
            // 重置指针
            stream.Seek(0, SeekOrigin.Begin);
            // 从流读取字节块存入data中
            stream.Read(data, 0, Convert.ToInt32(stream.Length));
            return data;
        }
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值