当前位置: 首页 > news >正文

Unity添加GIF动画

国外网站上看到的一篇帖子

http://wiki.unity3d.com/index.php/AnimatedGifDrawer

它使用“图像”和“图形”的系统。的命名空间所以需要“system.Drawing.dll要导入的文件/添加到项目中

使用前准备:

1)复制"System.Drawing.dll" file in the "C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0"文件到"Assets" 文件夹下面。

2)创建一个新的脚本命名为“animatedgifdrawer下面的内容

3)将这个脚本场景中的任何对象

4)更改脚本”loadinggifpath”字段,你的GIF文件的路径。这可以从根项目文件夹是相对的也可以是绝对的。

下面给出C#代码:

 

using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using UnityEngine;
 
public class AnimatedGifDrawer : MonoBehaviour
{
    public string loadingGifPath;
    public float speed = 1;
    public Vector2 drawPosition;
 
    List<Texture2D> gifFrames = new List<Texture2D>();
    void Awake()
    {
        var gifImage = Image.FromFile(loadingGifPath);
        var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
        int frameCount = gifImage.GetFrameCount(dimension);
        for (int i = 0; i < frameCount; i++)
        {
            gifImage.SelectActiveFrame(dimension, i);
            var frame = new Bitmap(gifImage.Width, gifImage.Height);
            System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);
            var 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(frame.Width - 1 - x, y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped
                }
            frameTexture.Apply();
            gifFrames.Add(frameTexture);
        }
    }
 
    void OnGUI()
    {
        GUI.DrawTexture(new Rect(drawPosition.x, drawPosition.y, gifFrames[0].width, gifFrames[0].height), gifFrames[(int)(Time.frameCount * speed) % gifFrames.Count]);
    }
}

相关文章:

  • unity3d读取csv文件
  • unity3D 自定义显示中文
  • Eclipse中将class。Java打包成Jar
  • Java Web实习笔记
  • JAVA中indexOf函数用法和subString()方法
  • MyEclipse+Tomcat + Servlet开发
  • Servlet学习
  • JAVA中Iterator的具体作用?
  • 实习日志(3)
  • FileItem类
  • JSP中EL表达式语言不能使用的解决方法
  • myeclipse中文乱码,JSP页面乱码
  • Tomcat中文乱码问题的原理和解决方法
  • java的web.xml中出现ERROR:Editor model does not support child element taglib of web
  • quote symbol expected异常
  • 【Leetcode】104. 二叉树的最大深度
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • CentOS 7 修改主机名
  • cookie和session
  • Cumulo 的 ClojureScript 模块已经成型
  • echarts花样作死的坑
  • ES6 ...操作符
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • Idea+maven+scala构建包并在spark on yarn 运行
  • isset在php5.6-和php7.0+的一些差异
  • Java基本数据类型之Number
  • JS变量作用域
  • Laravel5.4 Queues队列学习
  • php中curl和soap方式请求服务超时问题
  • Python - 闭包Closure
  • ReactNativeweexDeviceOne对比
  • SegmentFault 2015 Top Rank
  • Solarized Scheme
  • SQL 难点解决:记录的引用
  • Travix是如何部署应用程序到Kubernetes上的
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 翻译--Thinking in React
  • 好的网址,关于.net 4.0 ,vs 2010
  • 每天一个设计模式之命令模式
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 用Visual Studio开发以太坊智能合约
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • ​批处理文件中的errorlevel用法
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • $NOIp2018$劝退记
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (Git) gitignore基础使用
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (Matlab)使用竞争神经网络实现数据聚类
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (一) storm的集群安装与配置
  • (转)memcache、redis缓存