這篇文章將為大家詳細(xì)講解有關(guān)Unity3D如何實(shí)現(xiàn)播放gif圖功能,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供廣德網(wǎng)站建設(shè)、廣德做網(wǎng)站、廣德網(wǎng)站設(shè)計(jì)、廣德網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、廣德企業(yè)網(wǎng)站模板建站服務(wù),10多年廣德做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
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; } } }
關(guān)于“Unity3D如何實(shí)現(xiàn)播放gif圖功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
分享題目:Unity3D如何實(shí)現(xiàn)播放gif圖功能
標(biāo)題URL:http://vcdvsql.cn/article32/gjejpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、定制開(kāi)發(fā)、外貿(mào)建站、響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)