Пример #1
0
        public static SequenceFrame[] GetFrames(byte[] bytes)
        {
            var gifData = new GifData(bytes);

            GetFramesVoid(gifData);
            return(gifData.Frames);
        }
Пример #2
0
        IEnumerator DecodeAndPlay()
        {
            if (!GifAsset)
            {
                Debug.LogError("UnityGif@" + name + ": Gif asset is null, please check gif asset SerializeField !");
                yield break;
            }

            var gifData = new GifData(GifAsset.bytes);

            yield return(GifHelper.GetFramesEnumerator(gifData));

            if (gifData.Frames == null)
            {
                Debug.Log("Gif decoding by Coroutine does not effect, use no-Coroutine instead !");
                GifHelper.GetFramesVoid(gifData);
            }
            Frames = gifData.Frames;
            StartCoroutine(PlayNextFrame());
        }
Пример #3
0
        public static void GetFramesVoid(GifData gifData)
        {
            //初始化GIF
            var gif = new GraphicsInterchangeFormat(gifData.Bytes);

            //借助组件单帧加载可以不考虑了,因为Gif需要借助上一帧

            //序列帧集初始化
            gifData.Frames = new SequenceFrame[gif.FrameImageDescriptors.Length];
            //初始化Texture
            var frameTexture = new Texture2D(gif.Width, gif.Height);

            //透明背景
            var transparentPixels = frameTexture.GetPixels32();

            for (var index = 0; index < transparentPixels.Length; index++)
            {
                transparentPixels[index] = Color.clear;
            }

            //背景色
            var backgroundColor  = gif.GetPixel(gif.BgColorIndex);
            var backgroundPixels = frameTexture.GetPixels32();

            for (var index = 0; index < backgroundPixels.Length; index++)
            {
                backgroundPixels[index] = backgroundColor;
            }

            //记录下一帧的处理方法
            bool previousReserved = false;

            //处理每个图块
            for (var frameIndex = 0; frameIndex < gifData.Frames.Length; frameIndex++)
            {
                //命名
                frameTexture.name = "FrameOfIndex" + frameIndex;
                //图像描述器
                var frameImageDescriptor = gif.FrameImageDescriptors[frameIndex];
                //绘图控制扩展
                var frameGraphicController = gif.FrameGraphicControllers[frameIndex];

                //着色范围
                var blockWidth  = frameImageDescriptor.Width;
                var blockHeight = frameImageDescriptor.Height;

                var leftIndex   = frameImageDescriptor.MarginLeft;             //含
                var rightBorder = leftIndex + blockWidth;                      //不含

                var topBorder   = gif.Height - frameImageDescriptor.MarginTop; //不含
                var bottomIndex = topBorder - blockHeight;                     //含

                //色表
                var descriptorPixels = frameImageDescriptor.GetPixels(frameGraphicController, gif);
                //色表指针
                var colorIndex = -1;
                //gif的y是从上往下,texture的y是从下往上
                for (var y = topBorder - 1; y >= bottomIndex; y--)
                {
                    for (var x = leftIndex; x < rightBorder; x++)
                    {
                        colorIndex++;
                        //判断是否保留像素
                        if (previousReserved && descriptorPixels[colorIndex].a == 0)
                        {
                            continue;
                        }
                        frameTexture.SetPixel(x, y, descriptorPixels[colorIndex]);
                    }
                }

                //保存
                frameTexture.wrapMode = TextureWrapMode.Clamp;
                frameTexture.Apply();

                //添加序列帧,并兵初始化Texture
                gifData.Frames[frameIndex] = new SequenceFrame(frameTexture, frameGraphicController.DelaySeconds);

                //预处理下一帧图像
                previousReserved = false;
                frameTexture     = new Texture2D(gif.Width, gif.Height);
                switch (frameGraphicController.NextFrameDisposalMethod)
                {
                case NextFrameDisposalMethod.Bg:
                    frameTexture.SetPixels32(backgroundPixels);
                    break;

                case NextFrameDisposalMethod.Previous:
                    frameTexture.SetPixels32(gifData.Frames[frameIndex - 1].Texture.GetPixels32());
                    previousReserved = true;
                    break;

                default:
                    frameTexture.SetPixels32(gifData.Frames[frameIndex].Texture.GetPixels32());
                    previousReserved = true;
                    break;
                }
            }
        }