zoukankan      html  css  js  c++  java
  • unity播放gif动画

    using UnityEngine;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    /*
     这里引用的System.Drawing是在C:Program Files (x86)UnityEditorDataMonolibmono2.0目录下,这个是unity安装的目录;
     */
    public class GiftToTexture : MonoBehaviour
    {
        public UITexture GifTexture;
        private List<Texture2D> _mTexture2Ds = new List<Texture2D>();
        private float _mTime;
        private float _mSpeed = 5.0f;
        // Use this for initialization
        void Start()
        {
            string path = Application.dataPath;
            Image image = Image.FromFile(path+@"/texture.gif");
            _mTexture2Ds = GifToTextureByCS(image);
        }
    
        // Update is called once per frame
        void Update()
        {
            if (null != GifTexture && _mTexture2Ds.Count > 0)
            {
                _mTime += Time.deltaTime;
                int index = (int)(_mTime * _mSpeed) % _mTexture2Ds.Count;
                GifTexture.mainTexture = _mTexture2Ds[index];
            }
        }
    
        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放在Plugins文件夹下即可

  • 相关阅读:
    Three.js黑暗中的萤火虫
    Three.js会飞走飞回来的鸟
    Three.js响应和移动物体
    Three.js加载gltf模型
    Three.js创建运动立体几何体示例
    activemq artemis安装运行及其在springboot中的使用
    activemq安装运行及其在springboot中的queue和topic使用
    springboot成神之——mybatis和mybatis-generator
    java成神之——安全和密码
    java成神之——网络编程基本操作
  • 原文地址:https://www.cnblogs.com/MrZivChu/p/unityGif.html
Copyright © 2011-2022 走看看