您的位置:首页 > 移动开发 > Unity3D

Unity分解Gif为List<Texture>

2018-03-21 22:29 627 查看
1、将 Editor\Data\Mono\lib\mono\2.0\System.Drawing.dll
复制到 Assets\Library\System.Drawing.dll
2、应用以下代码using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using UnityEngine;

public class GifUtil
{
/// <summary>
/// 分解GIF
/// </summary>
public static List<Texture2D> GetTextures(string path)
{
List<Texture2D> textures = new List<Texture2D>();

Image gif = Image.FromFile(path);
if (gif == null)
return textures;

FrameDimension dimension = new FrameDimension(gif.FrameDimensionsList[0]);
for (int index = 0; index < gif.GetFrameCount(dimension); index++)
{
gif.SelectActiveFrame(dimension, index);
MemoryStream stream = new MemoryStream();
gif.Save(stream, ImageFormat.Png);
Texture2D texture = new Texture2D(gif.Width, gif.Height);
byte[] bytes = stream.ToArray();
texture.LoadImage(stream.ToArray());
//texture.LoadRawTextureData(bytes);
stream.Close();
textures.Add(texture);
}

return textures;
}
}
代码比较简单,就不写注释了,粘上应该0报错,报错请留言
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity gif texture