Unity解析gif動(dòng)態(tài)圖操作
工作需求,要播放一張gif圖片,又不想轉(zhuǎn)成視頻播放,就開始研究怎樣解析gif,在網(wǎng)上也看了不少教程,最后根據(jù)自己需求寫了個(gè)腳本。
首先,Unity是不支持gif的(至少我沒找到方法),而又要在NGUI中顯示gif圖片。所以就想到了將gif解析成序列幀再去循環(huán)播放。
有人說可以找軟件解析,然后導(dǎo)入U(xiǎn)nity做動(dòng)畫,最終我沒有采用,自己再Unity中以代碼解析,然后播放的。
代碼如下
(在Awake中解析的,因?yàn)橐谄渌_本調(diào)用,實(shí)時(shí)解析的話,到時(shí)候會(huì)花費(fèi)一會(huì)時(shí)間):
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>();//存儲(chǔ)解析出來的圖片
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>
/// 播放動(dòng)畫
/// </summary>
public void StartAni()
{
isPlay = true;
}
/// <summary>
/// 停止動(dòng)畫
/// </summary>
public void StopAni()
{
isPlay = false;
i = 0;
}
}
補(bǔ)充:Unity播放GIF插件,不使用第三方庫(kù),基于文件協(xié)議,純代碼實(shí)現(xiàn),兼容移動(dòng)端和序列幀
本人通過分析GIF的文件協(xié)議,分解GIF的各序列幀,然后封裝成Unity可使用的Texture,通過遞歸播放,實(shí)現(xiàn)了在Unity上播放GIF的功能,并發(fā)布到了AssetStore上面,歡迎各位朋友交流經(jīng)驗(yàn)。
核心源碼:
分解GIF
//處理每個(gè)圖塊
for (int index = 0; index < gif.GraphicControlExtensions.Count; index++)
{
//命名
textureDescriptor.name = "Frame" + (index + 1);
//圖像描述器
ImageDescriptor imageDescriptor = gif.ImageDescriptors[index];
//像素色號(hào)集
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;
}
//幀序號(hào)
mFrameIndex = mFrameIndex % mFrames.Count;
//繪圖
if (mRawImage)
mRawImage.texture = mFrames[mFrameIndex].Texture;
if (mImage)
mImage.sprite = mFrames[mFrameIndex].Sprite;
//幀延時(shí)
yield return new WaitForSeconds(mFrames[mFrameIndex].DelaySecond);
//序號(hào)++
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\中。
歡迎使用。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- Unity3D 單例模式和靜態(tài)類的使用詳解
- Unity3d 如何更改Button的背景色
- unity avprovideo插件的使用詳解
- unity里獲取text中文字寬度并截?cái)嗍÷缘牟僮?/a>
- Unity3D啟動(dòng)外部程序并傳遞參數(shù)的實(shí)現(xiàn)
- unity 如何獲取button文本的內(nèi)容
- unity 如何獲取Text組件里text內(nèi)容的長(zhǎng)度
- unity 如何使用文件流讀取streamingassets下的資源
- unity 文件流讀取圖片與www讀取圖片的區(qū)別介紹
- Unity使用物理引擎實(shí)現(xiàn)多旋翼無人機(jī)的模擬飛行
相關(guān)文章
C#使用Win2D在UWP程序中實(shí)現(xiàn)2D繪圖
這篇文章介紹了C#使用Win2D在UWP程序中實(shí)現(xiàn)2D繪圖的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼
這篇文章主要介紹了C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
C#實(shí)現(xiàn)動(dòng)態(tài)執(zhí)行字符串腳本(優(yōu)化版)的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)動(dòng)態(tài)執(zhí)行字符串腳本(優(yōu)化版),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解
這篇文章主要介紹了WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C#如何在窗體程序中操作數(shù)據(jù)庫(kù)數(shù)據(jù)
這篇文章主要介紹了C#如何在窗體程序中操作數(shù)據(jù)庫(kù)數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
C#實(shí)現(xiàn)閃動(dòng)托盤圖標(biāo)效果的方法
這篇文章主要介紹了C#實(shí)現(xiàn)閃動(dòng)托盤圖標(biāo)效果的方法,涉及C# ImageList控件的使用技巧,需要的朋友可以參考下2016-06-06
Unity3D舊電視濾鏡shader的實(shí)現(xiàn)示例
這篇文章主要介紹了Unity3D舊電視濾鏡shader的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Unity Undo實(shí)現(xiàn)原理和使用方法詳解
本文將詳細(xì)介紹Unity Undo實(shí)現(xiàn)原理和使用方法,并提供多個(gè)使用例子,幫助開發(fā)者更好地理解和應(yīng)用該功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C# winformTextBox 鍵盤監(jiān)聽方式
這篇文章主要介紹了C# winformTextBox 鍵盤監(jiān)聽方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04

