服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity解析gif动态图操作

Unity解析gif动态图操作

2022-11-12 15:08末零 C#

这篇文章主要介绍了Unity解析gif动态图操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

工作需求,要播放一张gif图片,又不想转成视频播放,就开始研究怎样解析gif,在网上也看了不少教程,最后根据自己需求写了个脚本。

首先,Unity是不支持gif的(至少我没找到方法),而又要在NGUI中显示gif图片。所以就想到了将gif解析成序列帧再去循环播放。

有人说可以找软件解析,然后导入Unity做动画,最终我没有采用,自己再Unity中以代码解析,然后播放的。

代码如下

(在Awake中解析的,因为要在其他脚本调用,实时解析的话,到时候会花费一会时间):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
 
using UnityEngine;
 
public class AnimatedGifDrawer : MonoBehaviour
{
    public string loadingGifPath;//路径
    public UITexture tex;//图片
    public float speed = 0.1f;//播放速度
 
    private bool isPlay = false;//是否播放
    private int i = 0;//控制要播放的图片
 
    private List<Texture2D> gifFrames = new List<Texture2D>();//存储解析出来的图片
    void Awake()
    {
        Image gifImage = Image.FromFile(loadingGifPath);
        FrameDimension dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
        int frameCount = gifImage.GetFrameCount(dimension);
        for (int i = 0; i < frameCount; i++)
        {
            gifImage.SelectActiveFrame(dimension, i);
            Bitmap frame = new Bitmap(gifImage.Width, gifImage.Height);
            System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);
            Texture2D frameTexture = new Texture2D(frame.Width, frame.Height);
            for (int x = 0; x < frame.Width; x++)
                for (int y = 0; y < frame.Height; y++)
                {
                    System.Drawing.Color sourceColor = frame.GetPixel(x, y);
                    frameTexture.SetPixel( x, frame.Height - 1 - y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped
                }
            frameTexture.Apply();
            gifFrames.Add(frameTexture);
        }
    }
 
    private void Update()
    {
        if (isPlay == true)
        {
            i++;
            tex.mainTexture = gifFrames[(int)(i * speed) % gifFrames.Count];
        }   
    }
 
    /// <summary>
    /// 播放动画
    /// </summary>
    public void StartAni()
    {
        isPlay = true;
    }
 
    /// <summary>
    /// 停止动画
    /// </summary>
    public void StopAni()
    {
        isPlay = false;
        i = 0;
    }
}

补充:Unity播放GIF插件,不使用第三方库,基于文件协议,纯代码实现,兼容移动端和序列帧

本人通过分析GIF的文件协议,分解GIF的各序列帧,然后封装成Unity可使用的Texture,通过递归播放,实现了在Unity上播放GIF的功能,并发布到了AssetStore上面,欢迎各位朋友交流经验。

核心源码:

分解GIF

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//处理每个图块
            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;
                }
            }

递归播放

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <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\中。

欢迎使用。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/n_moling/article/details/76682392

延伸 · 阅读

精彩推荐
  • C#C#小数点格式化用法小结

    C#小数点格式化用法小结

    这篇文章主要介绍了C#小数点格式化用法,结合实例形式分析了C#小数点格式化运算的常用函数与相关使用技巧,需要的朋友可以参考下...

    Quber9942021-12-02
  • C#C# TSC打印二维码和条形码的实现方法

    C# TSC打印二维码和条形码的实现方法

    下面小编就为大家分享一篇C# TSC打印二维码和条形码的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    叶长种6952022-02-17
  • C#c#添加Newtonsoft.Json包的操作

    c#添加Newtonsoft.Json包的操作

    这篇文章主要介绍了c#添加Newtonsoft.Json包的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    yuanfujie9462022-10-27
  • C#C#访问SQL Server数据库的实现方法

    C#访问SQL Server数据库的实现方法

    这篇文章主要介绍了C#访问SQL Server数据库的实现方法,以实例形式简单分析了C#连接、查询SQL Server数据库的具体技巧,具有一定参考借鉴价值,需要的朋友可以...

    weiren20067952021-10-28
  • C#详解C#编程获取资源文件中图片的方法

    详解C#编程获取资源文件中图片的方法

    这篇文章主要介绍了详解C#编程获取资源文件中图片的方法的相关资料,需要的朋友可以参考下...

    123si3752022-01-06
  • C#轻松学习C#的抽象类

    轻松学习C#的抽象类

    轻松学习C#的抽象类,对C#的抽象类感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的抽象类。...

    丿木呈广予口贝10032021-11-04
  • C#如何使用Dapper处理多个结果集与多重映射实例教程

    如何使用Dapper处理多个结果集与多重映射实例教程

    Dapper类是一个开源的数据库操作类,下面这篇文章主要给大家介绍了关于如何使用Dapper处理多个结果集与多重映射的相关资料,文中通过示例代码介绍的非...

    省厓3512022-03-01
  • C#C#验证码问题的快速解决方法

    C#验证码问题的快速解决方法

    下面小编就为大家带来一篇C#验证码问题的快速解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网10622021-12-01