unity  嵌入gif动画

一直在纠结swf,然后在网上看到一篇国外的帖子,便解决这平面动画嵌入unity的问题了。

首先复制”"System.Drawing.dll" file in the "C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0"文件到"Assets" 文件夹下面。​

然后新建一个场景,新建一个playgif类,然后将此类挂在摄像机上。最后附上代码

using System.Collections.Generic;

using System.Drawing;

using System.Drawing.Imaging;

using UnityEngine;

public class playgif : MonoBehaviour {

public string loadingGifPath;

public float speed = 0.05f;

public Vector2 drawPosition;

List gifFrames = new List();

void Awake()

{

var gifImage = Image.FromFile(loadingGifPath);

var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);

int frameCount = gifImage.GetFrameCount(dimension);

for (int i = 0; i < frameCount; i++)

{

gifImage.SelectActiveFrame(dimension, i);

var frame = new Bitmap(gifImage.Width, gifImage.Height);

System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);

var frameTexture = new Texture2D(frame.Width, frame.Height);

for (int x = 0; x < frame.Width; x++)

for (int y = 0; y < frame.Height; y++)

{

System.Drawing.Color sourceColor = frame.GetPixel(x, y);

frameTexture.SetPixel(frame.Width - 1 - x, y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped

}

frameTexture.Apply();

gifFrames.Add(frameTexture);

}

}

void OnGUI()

{

GUI.DrawTexture(new Rect(drawPosition.x, drawPosition.y, gifFrames[0].width, gifFrames[0].height), gifFrames[(int)(Time.frameCount * speed) % gifFrames.Count]);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值