Unity3D實(shí)現(xiàn)播放gif圖功能
Unity是不識(shí)別Gif格式圖的,需要我們使用c#將gif里多幀圖轉(zhuǎn)化為Texture2D格式。需要使用System.Drawing.dll.此dll在unity安裝目錄下就可以找到。由于unity沒(méi)有g(shù)if格式的文件,所以我們無(wú)法在面板指定,需要?jiǎng)討B(tài)加載。所以將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("圖片張數(shù):" + image.FrameDimensionsList.Length); FrameDimension frame = new FrameDimension(image.FrameDimensionsList[0]); int framCount = image.GetFrameCount(frame);//獲取維度幀數(shù) 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); // 創(chuàng)建一個(gè)字節(jié)數(shù)組,長(zhǎng)度為流的長(zhǎng)度 byte[] data = new byte[stream.Length]; // 重置指針 stream.Seek(0, SeekOrigin.Begin); // 從流讀取字節(jié)塊存入data中 stream.Read(data, 0, Convert.ToInt32(stream.Length)); return data; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Unity 如何獲取鼠標(biāo)停留位置下的物體
- Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作
- 解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑
- unity AudioSource播放完聲音后要執(zhí)行的函數(shù)或條件操作
- unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
- Unity3d 使用Gizmos畫一個(gè)圓圈
- unity 如何使用LineRenderer 動(dòng)態(tài)劃線
- Unity 通過(guò)LineRenderer繪制兩點(diǎn)之間的直線操作
- Unity 實(shí)現(xiàn)給物體替換材質(zhì)球
- Unity解析gif動(dòng)態(tài)圖操作
相關(guān)文章
分享一個(gè)C#編寫簡(jiǎn)單的聊天程序(詳細(xì)介紹)
這是一篇基于Socket進(jìn)行網(wǎng)絡(luò)編程的入門文章,我對(duì)于網(wǎng)絡(luò)編程的學(xué)習(xí)并不夠深入,這篇文章是對(duì)于自己知識(shí)的一個(gè)鞏固,同時(shí)希望能為初學(xué)的朋友提供一點(diǎn)參考。文章大體分為四個(gè)部分:程序的分析與設(shè)計(jì)、C#網(wǎng)絡(luò)編程基礎(chǔ)(篇外篇)、聊天程序的實(shí)現(xiàn)模式、程序?qū)崿F(xiàn)2015-12-12c# 使用特定帳號(hào)密碼訪問(wèn)Windows網(wǎng)路共享
這篇文章主要介紹了c# 使用特定帳號(hào)密碼訪問(wèn)Windows網(wǎng)路共享的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C#如何動(dòng)態(tài)創(chuàng)建lambda表達(dá)式
這篇文章主要介紹了C#如何動(dòng)態(tài)創(chuàng)建lambda表達(dá)式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02C#多線程開(kāi)發(fā)之任務(wù)并行庫(kù)詳解
最近在學(xué)習(xí)C#的并行編程,在每本書上的看到的知識(shí)點(diǎn)都不全面,所以先參考多本書書籍的講解,將并行編程,多線程編程的知識(shí)點(diǎn)整理一下,這篇文章主要給大家介紹了關(guān)于C#多線程開(kāi)發(fā)之任務(wù)并行庫(kù)的相關(guān)資料,需要的朋友可以參考下2021-09-09c# 幾種常見(jiàn)的加密方法的實(shí)現(xiàn)
這篇文章主要介紹了c# 幾種常見(jiàn)的加密方法的實(shí)現(xiàn),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12C#從windows剪貼板獲取并顯示文本內(nèi)容的方法
這篇文章主要介紹了C#從windows剪貼板獲取并顯示文本內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入
這篇文章主要介紹了c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入的步驟,幫助大家更好的理解和使用c#中的Dictionary類,感興趣的朋友可以了解下2021-02-02C#使用struct直接轉(zhuǎn)換下位機(jī)數(shù)據(jù)的示例代碼
這篇文章主要介紹了C#使用struct直接轉(zhuǎn)換下位機(jī)數(shù)據(jù)的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01