Unity制作GIF动图教程 VR资源

仅为年时 2017-12-07 17:40:23
由于项目loading界面设计了一个gif图效果不错,但是unity本身并不支持该格式,所以就想办法进行播放。

 



本来是在商店下载了一个插件,但是没用明白,然后看了下网上的一些方法,都是逐帧播放,和插件大同小异,所以就借用网上的代码,只是稍微修改成UGUI的样式来播放(网上都是NGUI的)。
 
 
01
02
03
04
05
06
07
08
09
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
65
66
67
68
69
70
using UnityEngine;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
 
public class GifToSprite : MonoBehaviour
{
    private List<Texture2D> _mTexture2Ds = new List<Texture2D>();
    private float _mTime;
    private float _mSpeed = 5.0f;
 
    UnityEngine.UI.Image gifTex;
    // Use this for initialization
    void Start()
    {
        gifTex = GetComponent<UnityEngine.UI.Image>();
 
        Image image = Image.FromFile(Application.dataPath + "/StreamingAssets/GIF.gif");
        _mTexture2Ds = GifToTextureByCS(image);
    }
 
    // Update is called once per frame
    void Update()
    {
        if(null != gifTex && _mTexture2Ds.Count > 0)
        {
            _mTime += 3 * Time.deltaTime;
            int index = (int)(_mTime * _mSpeed) % _mTexture2Ds.Count;
            //new Rect(0, 0, 769, 239)后面两个数字是图片的分辨率
            gifTex.overrideSprite = Sprite.Create(_mTexture2Ds[index], new Rect(0, 0, 769, 239), Vector2.zero);
        }
    }
 
    List<Texture2D> GifToTextureByCS(Image image)
    {
        List<Texture2D> texture2D = null;
        if (null != image)
        {
            texture2D = new List<Texture2D>();
            //Debug.LogError(image.FrameDimensionsList.Length);
            //image.FrameDimensionsList.Length = 1;
            //根据指定的唯一标识创建一个提供获取图形框架维度信息的实例;
            FrameDimension frameDimension = new FrameDimension(image.FrameDimensionsList[0]);
            //获取指定维度的帧数;
            int framCount = image.GetFrameCount(frameDimension);
            for (int i = 0; i < framCount; i++)
            {
                //选择由维度和索引指定的帧;
                image.SelectActiveFrame(frameDimension, i);
                var framBitmap = new Bitmap(image.Width, image.Height);
                //从指定的Image 创建新的Graphics,并在指定的位置使用原始物理大小绘制指定的 Image;
                //将当前激活帧的图形绘制到framBitmap上;
                System.Drawing.Graphics.FromImage(framBitmap).DrawImage(image, Point.Empty);
                var frameTexture2D = new Texture2D(framBitmap.Width, framBitmap.Height);
                for (int x = 0; x < framBitmap.Width; x++)
                {
                    for (int y = 0; y < framBitmap.Height; y++)
                    {
                        //获取当前帧图片像素的颜色信息;
                        System.Drawing.Color sourceColor = framBitmap.GetPixel(x, y);
                        //设置Texture2D上对应像素的颜色信息;
                        frameTexture2D.SetPixel(x, framBitmap.Height - 1 - y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A));
                    }
                }
                frameTexture2D.Apply();
                texture2D.Add(frameTexture2D);
            }
        }
        return texture2D;
    }
}
大家贴上这段代码是肯定不好用的,因为我们缺少一个动态链接库,System.Drawing.dll。这个库在上面的插件里有,或者直接度娘搜也可以。
99VR视界二维码
热门推荐
Hot Recommended
在线客服