ImageFrameAnimation.cs

 using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; [RequireComponent(typeof(Image))]
public class ImageFrameAnimation : MonoBehaviour { private Image image;
private int nowFrame = ;
private float timer = ; private List<Sprite> spriteList;
private bool isPlay = false;
private bool isForward = true;
private bool isLoop = true;
private float interval = 0.1f; void Update ()
{
if (!isPlay || (spriteList.Count == ))
{
return;
} timer += Time.deltaTime;
if (timer > interval)
{
timer = ;
if (isForward)
{
nowFrame++;
}
else
{
nowFrame--;
} if (nowFrame >= spriteList.Count)
{
if (isLoop)
{
nowFrame = ;
}
else
{
isPlay = false;
return;
}
}
else if(nowFrame < )
{
if (isLoop)
{
nowFrame = spriteList.Count - ;
}
else
{
isPlay = false;
return;
}
} UpdateSprite();
}
} private void UpdateSprite()
{
UpdateSprite(nowFrame);
} private void UpdateSprite(int index)
{
image.sprite = spriteList[index];
} public void Init(Sprite[] spriteArray, bool isPlay = true, bool isForward = true, bool isLoop = true, float interval = 0.1f)
{
List<Sprite> spriteList = new List<Sprite>();
spriteList.AddRange(spriteArray);
Init(spriteList, isPlay, isForward, isLoop, interval);
} public void Init(List<Sprite> spriteList, bool isPlay = true, bool isForward = true, bool isLoop = true, float interval = 0.1f)
{
this.spriteList = spriteList;
this.isPlay = isPlay;
this.isForward = isForward;
this.isLoop = isLoop;
this.interval = interval; if (image == null)
{
image = GetComponent<Image>();
}
if (isForward)
{
nowFrame = ;
}
else
{
nowFrame = spriteList.Count - ;
}
timer = ; UpdateSprite();
} public void Play()
{
isPlay = true;
isForward = true;
} public void PlayReverse()
{
isPlay = true;
isForward = false;
} public void Pause()
{
isPlay = false;
}
}

测试:

 using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI; public class TestImageFrameAnimation : MonoBehaviour { public Image image;
private ImageFrameAnimation ani; void Start ()
{
ani = image.gameObject.AddComponent<ImageFrameAnimation>();
Sprite[] sprites = Resources.LoadAll<Sprite>("Num");
ani.Init(sprites, true, true, true, );
} private void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
ani.Pause();
} if (Input.GetKeyDown(KeyCode.W))
{
ani.PlayReverse();
}
}
}

效果:

[UGUI]帧动画的更多相关文章

  1. UGUI 帧动画插件

    最近在开发一款功夫猫游戏,本来使用Unity Sprite制作,但是发现Sprite对各种分辨率不支持. 看着游戏很简单就使用UGUI制作,在中途发现有很多帧动画播放,使用了Animation调整使用 ...

  2. 深入理解CSS3 Animation 帧动画

    CSS3我在5年之前就有用了,包括公司项目都一直在很前沿的技术. 最近在写慕课网的七夕主题,用了大量的CSS3动画,但是真的沉淀下来仔细的去深入CSS3动画的各个属性发现还是很深的,这里就写下关于帧动 ...

  3. Android动画效果之Frame Animation(逐帧动画)

    前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...

  4. android 帧动画,补间动画,属性动画的简单总结

      帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...

  5. android 帧动画

    首先在res/drawable/name1.xml/定义一组图片集合: <?xml version="1.0" encoding="utf-8"?> ...

  6. 3d图片切换(css3帧动画)

    效果带抖动翻转隐藏,使用帧动画 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  7. CoolPlist 帧动画自动生成工具

    工具英文名称:CoolPlist作者: 陈前帆 thinkingMan | sonny 邮箱: 625936034@qq.com | chenqianfan1@163.com电话: 136704713 ...

  8. uwp 图片切换动画 使用帧动画

    上一篇博客使用了Timer来实现图片的切换,@lindexi_gd讨论了一下性能,我本人其实对性能这一方面不太熟,但我觉得还是有必要考虑一下,那么今天我们使用帧动画开实现以下 新建项目,添加一个But ...

  9. android帧动画,移动位置,缩放,改变透明度等动画讲解

    1.苦逼的需求又来了,需要实现一些动画效果,第一个想到的是播放gif图片,但是这样会占包的资源,并且清晰度不高,于是想着程序实现,自己用帧动画+缩放+移动+透明度 实现了一些想要的效果,这里跟大家分享 ...

随机推荐

  1. Java JDBC基本用法

    准备oracle驱动包:maven添加oracle jdbc依赖 pom.xml <!-- oracle jdbc驱动 --> <dependency> <groupId ...

  2. Simple Logging Facade for Java 简单日志门面(Facade)

    SLF4J是为各种 loging APIs提供一个简单统一的接口,从而使得最终用户能够在部署的时候配置自己希望的loging APIs实现.Logging API实现既可以选择直接实现SLF4J接口的 ...

  3. JDK1.8中如何用ScriptEngine动态执行JS

    JDK1.8中如何用ScriptEngine动态执行JS jdk1.6开始就提供了动态脚本语言诸如JavaScript动态的支持.这无疑是一个很好的功能,毕竟Java的语法不是适合成为动态语言.而JD ...

  4. mig_7series DDR控制器的配置

    mig_7series DDR控制器的配置

  5. MySQL GTID 主从复制错误修复方法

    https://yq.aliyun.com/articles/155827?spm=5176.8067842.tagmain.6.RFPTAL MySQL 传统的主从复制方式使用 master_log ...

  6. ssl-openssl简介

    SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TLS与 ...

  7. HBase教程

    https://www.yiibai.com/hbase/ 自1970年以来,关系数据库用于数据存储和维护有关问题的解决方案.大数据的出现后,好多公司实现处理大数据并从中受益,并开始选择像 Hadoo ...

  8. java读写操作心得

    一.获得控制台用户输入的信息     public String getInputMessage() throws IOException...{         System.out.println ...

  9. Flask--异常处理

    异常处理: abort(404)-捕获HTTP抛出的统一状态码 @app.errorhandler-捕获全局异常错误码,捕获异常错误 @app.route("/demo4") de ...

  10. python中pip和pygame的安装

    1.安装pip和pygame都很简单.首先咱们来安装pip,官网https://pypi.python.org/pypi/pip#download,下载pip的压缩文件,并将其解压. 我们在cmd的p ...