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. mysql exists及not exists的使用

    对exists及not exists的使用根据下面的示例进行解释 如sql: select sname from student where exists (select * from score)) ...

  2. VSFTPD配置和错误 ,这篇文章也太厉害了

    配置过程 1.关闭防火墙  service iptables stop 2.启动vsftpd   service vsftpd start 3.添加root用权 cd /etc/vsftpd   更改 ...

  3. oracle--分组后获取每组数据第一条数据

    SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY cc.queuename ORDER BY cc.enroldate DESC) rn, cc ...

  4. 【转】[Android] NDK独立编译——独立工具链

    转载地址:https://blog.csdn.net/suningning/article/details/74510125

  5. 如何在Java中获取键盘输入值

    程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值的现成函数!Java没有提供这样的函数也不代表遇到这 ...

  6. 30 个java编程技巧(最佳实践的初学者)

    1.return 一个空的集合,而不是 null 如果一个程序返回一个没有任何值的集合,请确保一个空集合返回,而不是空元素.这样你就不用去写一大堆 ”if else” 判断null元素. Java 的 ...

  7. 【springBoot】之starter pom

    SpringBoot针对不同业务提供了不同的starter pom,根据springboot版本不同可能有差异. spring-boot-starter springboot核心starter ,包括 ...

  8. PHP在Linux下的套件LNMP

    LNMP官网:https://lnmp.org/install.html 另外不要忘了Azkaban和WhereHows

  9. C#DateTime好用但不常用的用法

    获取某年的某一个月天数 DateTime.DaysInMonth(year, i);

  10. 廖雪峰Java2面向对象编程-1面向对象-1面向对象基础

    1.对象的概念 面向对象编程:Object-Oriented Programming 对现实世界建立计算机模型的一种编程方法. 现实世界 计算机模型 Java代码 人 类/class class Pe ...