Unity解析gif動態(tài)圖操作
工作需求,要播放一張gif圖片,又不想轉(zhuǎn)成視頻播放,就開始研究怎樣解析gif,在網(wǎng)上也看了不少教程,最后根據(jù)自己需求寫了個腳本。
首先,Unity是不支持gif的(至少我沒找到方法),而又要在NGUI中顯示gif圖片。所以就想到了將gif解析成序列幀再去循環(huán)播放。
有人說可以找軟件解析,然后導(dǎo)入U(xiǎn)nity做動畫,最終我沒有采用,自己再Unity中以代碼解析,然后播放的。
代碼如下
(在Awake中解析的,因?yàn)橐谄渌_本調(diào)用,實(shí)時解析的話,到時候會花費(fèi)一會時間):
using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using UnityEngine; public class AnimatedGifDrawer : MonoBehaviour { public string loadingGifPath;//路徑 public UITexture tex;//圖片 public float speed = 0.1f;//播放速度 private bool isPlay = false;//是否播放 private int i = 0;//控制要播放的圖片 private List<Texture2D> gifFrames = new List<Texture2D>();//存儲解析出來的圖片 void Awake() { Image gifImage = Image.FromFile(loadingGifPath); FrameDimension dimension = new FrameDimension(gifImage.FrameDimensionsList[0]); int frameCount = gifImage.GetFrameCount(dimension); for (int i = 0; i < frameCount; i++) { gifImage.SelectActiveFrame(dimension, i); Bitmap frame = new Bitmap(gifImage.Width, gifImage.Height); System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty); Texture2D 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( x, frame.Height - 1 - y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped } frameTexture.Apply(); gifFrames.Add(frameTexture); } } private void Update() { if (isPlay == true) { i++; tex.mainTexture = gifFrames[(int)(i * speed) % gifFrames.Count]; } } /// <summary> /// 播放動畫 /// </summary> public void StartAni() { isPlay = true; } /// <summary> /// 停止動畫 /// </summary> public void StopAni() { isPlay = false; i = 0; } }
補(bǔ)充:Unity播放GIF插件,不使用第三方庫,基于文件協(xié)議,純代碼實(shí)現(xiàn),兼容移動端和序列幀
本人通過分析GIF的文件協(xié)議,分解GIF的各序列幀,然后封裝成Unity可使用的Texture,通過遞歸播放,實(shí)現(xiàn)了在Unity上播放GIF的功能,并發(fā)布到了AssetStore上面,歡迎各位朋友交流經(jīng)驗(yàn)。
核心源碼:
分解GIF
//處理每個圖塊 for (int index = 0; index < gif.GraphicControlExtensions.Count; index++) { //命名 textureDescriptor.name = "Frame" + (index + 1); //圖像描述器 ImageDescriptor imageDescriptor = gif.ImageDescriptors[index]; //像素色號集 byte[] colorIndexs = imageDescriptor.GetColorIndexs(); //繪圖控制擴(kuò)展 GraphicControlExtension control = gif.GraphicControlExtensions[index]; //像素指針 int pixelIndex = 0; //gif的像素點(diǎn)順序 左上到右下,unity的像素順序是 左下到右上,所以y套x, y翻轉(zhuǎn)一下 for (int y = imageDescriptor.MarginTop; y < imageDescriptor.MarginTop + imageDescriptor.Height; y++) { for (int x = imageDescriptor.MarginLeft; x < imageDescriptor.MarginLeft + imageDescriptor.Width; x++) { Color32 colorPixel = imageDescriptor.GetColor(colorIndexs[pixelIndex++], control, gif); if (colorPixel.a == 0 && reserve) continue; textureDescriptor.SetPixel(x, gif.Height - y - 1, colorPixel); } } //保存 textureDescriptor.Apply(); //添加序列幀 Sprite sprite = Sprite.Create(textureDescriptor, new Rect(0, 0, textureDescriptor.width, textureDescriptor.height), Vector2.zero); sprite.name = textureDescriptor.name; frames.Add(new UnityFrame(sprite, control.DelaySecond)); //初始化圖像 textureDescriptor = new Texture2D(gif.Width, gif.Height); reserve = false; //下一幀圖像預(yù)處理 switch (control.DisposalMethod) { //1 - Do not dispose. The graphic is to be left in place. //保留此幀 case DisposalMethod.Last: textureDescriptor.SetPixels(frames[index].Texture.GetPixels()); reserve = true; break; //2 - Restore to background color. The area used by the graphic must be restored to the background color. //還原成背景色 case DisposalMethod.Bg: textureDescriptor.SetPixels(textureBg.GetPixels()); break; //3 - Restore to previous. The decoder is required to restore the area overwritten by the graphic with what was there prior to rendering the graphic.//還原成上一幀 case DisposalMethod.Previous: textureDescriptor.SetPixels(frames[index - 1].Texture.GetPixels()); reserve = true; break; } }
遞歸播放
/// <summary> /// 遞歸播放 /// </summary> /// <returns></returns> IEnumerator Play() { if (mStop) { mFrameIndex = 0; yield break; } //幀序號 mFrameIndex = mFrameIndex % mFrames.Count; //繪圖 if (mRawImage) mRawImage.texture = mFrames[mFrameIndex].Texture; if (mImage) mImage.sprite = mFrames[mFrameIndex].Sprite; //幀延時 yield return new WaitForSeconds(mFrames[mFrameIndex].DelaySecond); //序號++ mFrameIndex++; //播放一次 if (!Loop && mFrameIndex == mFrames.Count) yield break; //遞歸播放下一幀 StartCoroutine(Play()); }
插件支持GIF播放和序列幀播放。 插件支持透明顏色。
插件通過GIF文件協(xié)議將圖像轉(zhuǎn)換為Unity支持的圖像,所有的實(shí)現(xiàn)都是通過C#代碼,所以你可以很容易的修改代碼,以達(dá)到你的需求。
插件支持Image和RawImage兩種組件,當(dāng)然你可以改造一下支持其他組件。
插件支持3種播放模式:
1、通過GIF的文件路徑
2、通過拖拽GIF的二進(jìn)制文件
3、通過拖拽序列幀
例子放在文件夾Assets\Plugin\GifPlayer\Dome\中。
歡迎使用。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- Unity3D 單例模式和靜態(tài)類的使用詳解
- Unity3d 如何更改Button的背景色
- unity avprovideo插件的使用詳解
- unity里獲取text中文字寬度并截?cái)嗍÷缘牟僮?/a>
- Unity3D啟動外部程序并傳遞參數(shù)的實(shí)現(xiàn)
- unity 如何獲取button文本的內(nèi)容
- unity 如何獲取Text組件里text內(nèi)容的長度
- unity 如何使用文件流讀取streamingassets下的資源
- unity 文件流讀取圖片與www讀取圖片的區(qū)別介紹
- Unity使用物理引擎實(shí)現(xiàn)多旋翼無人機(jī)的模擬飛行
相關(guān)文章
C#設(shè)置WinForm中DataGrid列的方法(列寬/列標(biāo)題等)
這篇文章主要介紹了C#設(shè)置WinForm中DataGrid列的方法,包括列寬、列標(biāo)題等部分,并分析了其中相關(guān)的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07C#使用委托(delegate)實(shí)現(xiàn)在兩個form之間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了C#使用委托(delegate)實(shí)現(xiàn)在兩個form之間傳遞數(shù)據(jù)的方法,涉及C#委托的使用技巧,需要的朋友可以參考下2015-04-04