Unity3D实现播放gif图功能
作者:JayW就是我吖 发布时间:2021-11-23 13:17:37
标签:Unity3D,gif
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;
}
}
}
来源:https://blog.csdn.net/qq_33994566/article/details/86534969


猜你喜欢
- 一、前言在项目中,我们有一些公共的字段需要做修改如:gmt_create:创建时间creator_id:创建人gmt_modified:修改
- 本文实例讲述了C#编程调用Cards.dll实现图形化发牌功能。分享给大家供大家参考,具体如下:using System;using Sys
- 1、使用AsyncTask异步任务实现,调用publishProgress()方法刷新进度来实现(已优化)public class MyAs
- Hibernate ThreadLocal它会为每个线程维护一个私有的变量空间。实际上, 其实现原理是在JVM 中维护一个Map,这个Map
- 注意:要先导入javamail的mail.jar包。以下三段代码是我的全部代码,朋友们如果想用,直接复制即可。第一个类:MailSender
- 在开发中,我们经常会使用IO操作,例如创建,删除文件等操作。在项目中这样的需求也较多,我们也会经常对这些操作进行编
- 缘起标准的RABC, 权限需要支持动态配置,spring security默认是在代码里约定好权限,真实的业务场景通常需要可以支持动态配置角
- 本文实例为大家分享了unity使用socket实现聊天室的具体代码,供大家参考,具体内容如下unity聊天室服务端实现using Syste
- 1、mybatis-plus @DS实现动态切换数据源原理首先mybatis-plus使用com.baomidou.dynamic.data
- 这篇文章主要介绍了Java使用Collections工具类对List集合进行排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一
- 整体步骤流程先来说一下整体的步骤思路吧:发送 UDP 广播,大家都知道 UDP 广播的特性是整个网段的设备都可以收到这个消息。接收方收到了
- 哈希表(HashMap)hash查询的时间复杂度是O(1)按值传递Character,Short,Integer,Long, Float,D
- 本文实例为大家分享了Java实现简单员工管理系统的具体代码,供大家参考,具体内容如下代码如下:import java.util.*;publ
- 下载IDEA插件IDEA插件选择版本号下载与IDEA版本号相近的插件打开IDEA,导入下载好的插件在File下找到setting在Plugi
- 本文实例为大家分享了java实现简单发红包的具体代码,供大家参考,具体内容如下这个案例是普通红包,均分的,不是拼手气红包。package n
- 导语如下图所示,这是一个导航选择弹框。进行单项选择,然后会监听回调选择的事件。问题是Android的RadioButton是一般是放在Rad
- 今天工作中遇到一个需求,就是获取 excel 里面的内容,并且把 excel 另存为 csv,因为本人以前未接触过,所以下面整理出来的代码均
- 1.拉取centos镜像docker pull centos:72.基于拉取到的镜像运行一个容器docker run -it --name
- 本文实例为大家分享了Android实现秒表功能的具体代码,供大家参考,具体内容如下设计完成一个秒表,具备启停功能,正确使用工作线程完成界面刷
- 1.Action中的validate()方法Struts2提供了一个Validateable接口,这个接口中只存在validat