[UGUI]帧动画
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]帧动画的更多相关文章
- UGUI 帧动画插件
最近在开发一款功夫猫游戏,本来使用Unity Sprite制作,但是发现Sprite对各种分辨率不支持. 看着游戏很简单就使用UGUI制作,在中途发现有很多帧动画播放,使用了Animation调整使用 ...
- 深入理解CSS3 Animation 帧动画
CSS3我在5年之前就有用了,包括公司项目都一直在很前沿的技术. 最近在写慕课网的七夕主题,用了大量的CSS3动画,但是真的沉淀下来仔细的去深入CSS3动画的各个属性发现还是很深的,这里就写下关于帧动 ...
- Android动画效果之Frame Animation(逐帧动画)
前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...
- android 帧动画,补间动画,属性动画的简单总结
帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...
- android 帧动画
首先在res/drawable/name1.xml/定义一组图片集合: <?xml version="1.0" encoding="utf-8"?> ...
- 3d图片切换(css3帧动画)
效果带抖动翻转隐藏,使用帧动画 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...
- CoolPlist 帧动画自动生成工具
工具英文名称:CoolPlist作者: 陈前帆 thinkingMan | sonny 邮箱: 625936034@qq.com | chenqianfan1@163.com电话: 136704713 ...
- uwp 图片切换动画 使用帧动画
上一篇博客使用了Timer来实现图片的切换,@lindexi_gd讨论了一下性能,我本人其实对性能这一方面不太熟,但我觉得还是有必要考虑一下,那么今天我们使用帧动画开实现以下 新建项目,添加一个But ...
- android帧动画,移动位置,缩放,改变透明度等动画讲解
1.苦逼的需求又来了,需要实现一些动画效果,第一个想到的是播放gif图片,但是这样会占包的资源,并且清晰度不高,于是想着程序实现,自己用帧动画+缩放+移动+透明度 实现了一些想要的效果,这里跟大家分享 ...
随机推荐
- 【java】private关键字
private关键字(1)修饰成员:可以修饰成员变量和成员方法(2)特点:被private修饰的后的成员只能在本类中被访问 (3)private的应用:定义类的时候,当把成员变量给private修饰 ...
- 2017.11.7 ant design - upload 组件的使用, react 条件渲染以及 axios.all() 的使用
一.主要任务:悉尼小程序管理后台,添加景点页面的开发 二.所遇问题及解决 1. 上传多个不同分类音频信息时,如中文音频和英文音频,要求音频不是放在一个数组中的,每个音频是一个独立的字段,此时: < ...
- Set up an SSH key with Sourcetree on macOS
1. 打开 sourcetree,进入设置-账户: 2. 选择添加账户,托管主机选择 Bitbucket(或Github),再连接账号,登录已有的 Bitbucket 账号(或 Github 账号), ...
- sublime text2+Ctags+Cscope替代Source Insight
说明:以Windows系统下查看C++代码为例.因为Source Insight(以下简称SI)是收费软件,且界面丑陋,所以考虑其替代方案,发现Sublime Text3(以下简称ST3) + Cta ...
- require的定义看不懂【2】
一段代码扔出来,前不着村后不着店的,各种全局变量 还有,现在还在纠结Id,url, 作者充其量也在写读书笔记,完全沿着自己思路走,也不管读者是否跟得上,这居然也能出书. 这一段,掌握源码,或者写过的人 ...
- BlockingQueue队列
1.BlockingQueue定义的常用方法如下 抛出异常 特殊值 阻塞 超时 插入 add(e) offer(e) put(e) offer(e,time,unit) 移除 remove() ...
- Microsoft.NET.Framework开机报错解决方法
win10自动更新后每次开机都报错Microsoft.NET.Framework. 如下图所示: 网上查了各种各样的方法折腾了好久. 其中看到了这样一个回答 “有两种可能 你电脑里的某个软件需要使用M ...
- C/C++基础---算法概览
符号概念 beg和end表示元素范围的迭代器 beg2表示第二个序列开始位置迭代器,end2表示第二个序列末尾迭代器(如果有).如没有则假定系列2至少与beg end表示的范围一样大.beg和beg2 ...
- 【ThreadLocal】使用ThreadLocal实现线程安全
非线程安全 public class UnSafeThreadLocalDemo { private int count = 0; public static void main(String[] a ...
- 【并发】基于 @Async和 CompletableFuture 实现并发异步操作
参考链接:Spring官方示例 User.java package hello; import com.fasterxml.jackson.annotation.JsonIgnorePropertie ...