Unity播放GIF插件,不使用第三方庫,基於文件協議,純代碼實現,兼容移動端和序列幀

本人通過分析GIF的文件協議,分解GIF的各序列幀,然後封裝成Unity可使用的Texture,通過遞歸播放,實現了在Unity上播放GIF的功能,併發布到了AssetStore上面,歡迎各位朋友交流經驗。

核心源碼:

分解GIF

            //處理每個圖塊
            for (int index = 0; index < gif.GraphicControlExtensions.Count; index++)
            {
                //命名
                textureDescriptor.name = "Frame" + (index + 1);

                //圖像描述器
                ImageDescriptor imageDescriptor = gif.ImageDescriptors[index];

                //像素色號集
                byte[] colorIndexs = imageDescriptor.GetColorIndexs();

                //繪圖控制擴展
                GraphicControlExtension control = gif.GraphicControlExtensions[index];

                //像素指針
                int pixelIndex = 0;

                //gif的像素點順序 左上到右下,unity的像素順序是 左下到右上,所以y套x, y翻轉一下
                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;

                //下一幀圖像預處理
                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文件協議將圖像轉換爲Unity支持的圖像,所有的實現都是通過C#代碼,所以你可以很容易的修改代碼,以達到你的需求。

 插件支持Image和RawImage兩種組件,當然你可以改造一下支持其他組件。

 插件支持3種播放模式:

 1、通過GIF的文件路徑 

2、通過拖拽GIF的二進制文件 

3、通過拖拽序列幀 

例子放在文件夾Assets\Plugin\GifPlayer\Dome\中。 

歡迎使用。


附:插件的UnityAssetStore鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章