Unity3D实现播放gif图功能
Unity是不识别Gif格式图的,需要我们使用c#将gif里多帧图转化为Texture2D格式。需要使用System.Drawing.dll.此dll在unity安装目录下就可以找到。由于unity没有gif格式的文件,所以我们无法在面板指定,需要动态加载。所以将gif图放在StreamingAssets文件夹下。以下为源代码:
usingSystem; usingSystem.Collections; usingSystem.Collections.Generic; usingSystem.Drawing; usingSystem.Drawing.Imaging; usingSystem.IO; usingUnityEngine; publicclassPlayGif:MonoBehaviour{ publicUnityEngine.UI.ImageIm; publicstringgifName=""; publicGameObject[]Ims; [SerializeField] privatefloatfps=5f; privateListtex2DList=newList (); privatefloattime; Bitmapmybitmp; voidStart() { System.Drawing.Imageimage=System.Drawing.Image.FromFile(Application.streamingAssetsPath+"/"+gifName+".gif"); tex2DList=MyGif(image); } //Updateiscalledonceperframe voidUpdate() { if(tex2DList.Count>0) { time+=Time.deltaTime; intindex=(int)(time*fps)%tex2DList.Count; if(Im!=null) { Im.sprite=Sprite.Create(tex2DList[index],newRect(0,0,tex2DList[index].width,tex2DList[index].height),newVector2(0.5f,0.5f)); } if(Ims.Length!=0) { for(inti=0;i ().material.mainTexture=tex2DList[index]; } } } privateList MyGif(System.Drawing.Imageimage) { List tex=newList (); if(image!=null) { //Debug.Log("图片张数:"+image.FrameDimensionsList.Length); FrameDimensionframe=newFrameDimension(image.FrameDimensionsList[0]); intframCount=image.GetFrameCount(frame);//获取维度帧数 for(inti=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。