Unity播放gif

unity不支持gif动图格式,因此建议使用序列帧的方式,我之前写过一份播放序列帧的文章,可以找找。如果非要使用序列帧,那就只能将图片加载出来,然后转成texture2d的格式,然后逐帧播放。只是这样会比较卡顿,不太容易被接收。

直接上代码,这个代码是转载别人的,还行,需要先打开你打开你当前使用unity的文件位置->Data->Mono->lib->mono->2.0->System.Drawing.dll 复制到你新建的工程的Assets目录下。

原文地址
unity播放gif

直接上代码

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

public class PlayGif : 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;

    int tatolCount = 0;
    bool isLoadOver = false;
        

    void Start()
    {
     
        
        Debug.Log("time1: " + Time.realtimeSinceStartup);
        System.Drawing.Image image = System.Drawing.Image.FromFile(Application.streamingAssetsPath + "/" + gifName + ".gif");
        Debug.Log("time2: " + Time.realtimeSinceStartup);


        //tex2DList = MyGif(image);  //此方法耗时太慢,3M的gif需要用时6.2秒,不能接受
        StartCoroutine(StartPlay(image));
        Debug.Log("time7: " + Time.realtimeSinceStartup);
    }

    IEnumerator StartPlay(Image image)
    {
     
        MyGif(image);
        while(!isLoadOver)
        {
     
            yield return null;
        }

    }

    // 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("time3: " + Time.realtimeSinceStartup);
            //Debug.Log("图片张数:" + image.FrameDimensionsList.Length);
            FrameDimension frame = new FrameDimension(image.FrameDimensionsList[0]);
            Debug.Log("time4: " + Time.realtimeSinceStartup);
            int framCount = image.GetFrameCount(frame);//获取维度帧数
            tatolCount = framCount;
            Debug.Log("time5: " + Time.realtimeSinceStartup);
            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);
            }
            Debug.Log("time6: " + Time.realtimeSinceStartup);
        }

        tex2DList = tex;
        isLoadOver = true;
        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;
        }
    }




}


这里因为处理图片,会很耗时,我查看了一下时间,加载一个3M的gif,总计需要6.9秒,基本是不能接收的。只能先偷偷加载好,然后用的时候再拿来播放。

最后要说一下,有传闻说这种方式打出来的包,在移动(android和ios)中貌似支持的不是很好,具体效果我没有试验。希望谁先试试,然后告诉我效果。

你可能感兴趣的:(Unity)