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. 【转】Linux安装HDF5及遇到的问题总结

    Linux安装HDF5及遇到的问题总结 转自: http://www.linuxdiyf.com/linux/26164.html   ubuntu版本:16.04.2 64位 从HDF官网(http ...

  2. 本地开发spark代码上传spark集群服务并运行

    打包 :右击.export.Java .jar File 把TestSpark.jar包上传到spark集群服务器的 spark_home下的myApp下: 提交spark任务: cd /usr/lo ...

  3. Qsys 设计流程---Qsys System Design Tutorial

    Qsys 设计流程 ---Qsys System Design Tutorial 1.Avalon-MM Pipeline Bridge Avalon-MM Pipeline Bridge在slave ...

  4. 自定义Write节点的afterrender属性

    由于nuke中的write节点提供了beforerender,afterrender这类事件,我们想添加一些功能只需要在这里面敲入代码即可.事件一旦发生,自然会触发我们敲入的co de.   Nuke ...

  5. hmaster 启动后自动关闭

    hbase重装后,hmaster却起不来,多次启动也不行,后来发现原因是在zookeeper中之前注册的hmaster仍然存在,系统中只允许一个hmaster运行.解决方法如下: 进入zk客户端,将h ...

  6. Visio View:打开VSd时,IE弹出已停止工作。

    解决办法:关闭第三方扩展功能

  7. vue中绑定多个class,多个情况使用同一个class时,用js的或 “||”

    class中 当依赖中断的时候是绑定的red,但是这样写绑定不了, 大括号{}里可以下js代码的,下面的orange就可以绑定成功,写成双竖线 形式.

  8. 【java】之常用四大线程池用法以及ThreadPoolExecutor详解

    为什么用线程池? 1.创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处-理效率2.线程并发数量过多,抢占系统资源从而导致阻塞3.对线程进行一些简单的管理 在Java中,线程池 ...

  9. 峰Redis学习(8)Redis 持久化AOF方式

    第三节:Redis 的持久化之AOF 方式 AOF方式:将以日志,记录每一个操作   优势:安全性相对RDB方式高很多: 劣势:效率相对RDB方式低很多: 1)AOF方式需要配置: # Please ...

  10. 【转】XP_cmdshell存储过程

    原文地址:http://www.cnblogs.com/love_study/archive/2010/03/02/1676583.html 一 .简介 xp_cmdshell 扩展存储过程将命令字符 ...