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两种组件,当然你可以改造一下支持其他组件。

 

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

欢迎使用。

附:插件的UnityAssetStore链接

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上海好程序员

给上海好程序员加个鸡腿!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值