unity中播放Gif图

底层还是将gif图分成了许多图片循环播放
将一下代码写到你自己创建的脚本中:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using UnityEngine;

public class GifDongTai : MonoBehaviour
{

    public UnityEngine.UI.Image Im;
    public string gifName = "";
    public GameObject[] Ims;
    [SerializeField]
    private float fps = 5f;
    private List<Texture2D> tex2DList = new List<Texture2D>();
    private float time;
    Bitmap mybitmp;
    void Start()
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(Application.streamingAssetsPath + "/" + gifName + ".gif");
        tex2DList = MyGif(image);
    }

    // Update is called once per frame
    void Update()
    {
        if (tex2DList.Count > 0)
        {
            time += Time.deltaTime;
            int index = (int)(time * fps) % tex2DList.Count;
            if (Im != null)
            {
                Im.sprite = Sprite.Create(tex2DList[index], new Rect(0, 0, tex2DList[index].width, tex2DList[index].height), new Vector2(0.5f, 0.5f));
            }
            if (Ims.Length != 0)
            {
                for (int i = 0; i < Ims.Length; i++)
                    Ims[i].GetComponent<Renderer>().material.mainTexture = tex2DList[index];

            }
        }
    }
    private List<Texture2D> MyGif(System.Drawing.Image image)
    {

        List<Texture2D> tex = new List<Texture2D>();
        if (image != null)
        {

            //Debug.Log("图片张数:" + image.FrameDimensionsList.Length);
            FrameDimension frame = new FrameDimension(image.FrameDimensionsList[0]);
            int framCount = image.GetFrameCount(frame);//获取维度帧数
            for (int i = 0; i < framCount; ++i)
            {

                image.SelectActiveFrame(frame, i);
                Bitmap framBitmap = new Bitmap(image.Width, image.Height);
                using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(framBitmap))
                {
                    graphic.DrawImage(image, Point.Empty);
                }
                Texture2D frameTexture2D = new Texture2D(framBitmap.Width, framBitmap.Height, TextureFormat.ARGB32, true);
                frameTexture2D.LoadImage(Bitmap2Byte(framBitmap));
                tex.Add(frameTexture2D);
            }
        }
        return tex;
    }
    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;
        }
    }
}

这里可能会报一些错误,多数都是与using System.Drawing;有关系,原因是.NET类库System.Drawing提供了一系列的图形函数,但由于其使用的是GDI接口,与DirectX和OpenGL之间不兼容,在Unity中默认是不被支持的。

解决方案:1、在Unity的安装路径中找到System.Drawing.dll,将其复制到我们的项目文件夹
System.Drawing.dll的具体位置:%Unity根目录%\Editor\Data\Mono\lib\mono\2.0\System.Drawing.dll

2、在Unity编辑器界面打开Player Settings面板,将Api Compatibility Level 从 “.NET 2.0 Subset” 改为 “.NET 2.0”
我的是这样的,我将其改为了.Net4.x
在这里插入图片描述

这样基本所报的错就消失了,然后脚本绑定到物体上,
在这里插入图片描述
设置合适的帧数,gif图的名字,UI的忙绑定到脚本。基本就完成了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值