Unity3D实现播放gif图功能

所属分类: 软件编程 / C#教程 阅读数: 64
收藏 0 赞 0 分享

Unity是不识别Gif格式图的,需要我们使用c#将gif里多帧图转化为Texture2D格式。需要使用System.Drawing.dll.此dll在unity安装目录下就可以找到。由于unity没有gif格式的文件,所以我们无法在面板指定,需要动态加载。所以将gif图放在StreamingAssets文件夹下。以下为源代码:

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;
  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;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

WPF仿三星手机充电界面实现代码

这篇文章主要为大家详细介绍了WPF仿三星手机充电界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

浅谈C#各种数组直接的数据复制/转换

下面小编就为大家带来一篇浅谈C#各种数组直接的数据复制/转换。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C#访问SQLServer增删改查代码实例

这篇文章主要为大家详细介绍了C#访问SQLServer增删改查代码实例,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#根据身份证号码判断出生日期和性别

这篇文章主要为大家详细介绍了C#根据身份证号码判断出生日期和性别的方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C# 向Word中设置/更改文本方向的方法(两种)

在一般情况下word中输入的文字都是横向的,今天小编给大家带来两种方法来设置更改文本方向的方法,非常不错,对c# word 更改文本方向的知识感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

让C# Excel导入导出 支持不同版本Office

让C# Excel导入导出,支持不同版本的Office,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#设置自定义文件图标实现双击启动(修改注册表)

这篇文章介绍的是利用C#设置自定义文件图标,然后实现双击启动的功能,文章给出了示例代码,介绍的很详细,有需要的可以参考借鉴。
收藏 0 赞 0 分享

C#两个相同属性的类赋值方法

这篇文章主要介绍了C#两个相同属性的类赋值方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中ListView控件实现窗体代码

这篇文章主要介绍了C#中ListView控件实现窗体的核心代码,非常不错,具有参考借鉴价值,对c#listview相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

浅谈C# 序列化与反序列化几种格式的转换

下面小编就为大家带来一篇浅谈C# 序列化与反序列化几种格式的转换。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多