Unity NGUI实现序列帧动画播放
如题,要实现序列帧的播放导入图片的时候需要注意:
(1)图片的命名要连续,如图:
(2)将这些图片在NGUI中打包成Altas图集的时候图片应该在同一个Altas中;
这里以播放特效为例,满足条件时播放特效,不满足条件时不播放特效。接下来可以创建一个Sprite,然后用代码控制序列帧特效的播放和停止:
播放:
if (something == false)
{
this._power_effect_sprite.GetComponent<UISprite>().enabled = true;
UISpriteAnimation uiAnim = _power_effect_sprite.AddComponent<UISpriteAnimation>(); // 设置图片的大小是否更改
uiAnim.Snap = false;
uiAnim.framesPerSecond = ;
this.isPlayAnimation = true;
}
停止:
if (this.isPlayAnimation == true)
{
Destroy(_power_effect_sprite.GetComponent<UISpriteAnimation>());
UISprite ui = _power_effect_sprite.GetComponent<UISprite>();
ui.spriteName = ui.atlas.spriteList[].name;
this._power_effect_sprite.GetComponent<UISprite>().enabled = false;
}
_power_effect_sprite表示创建的sprite,当满足条件时,代码会添加为sprite添加一个UISpriteAnimation.cs脚本来控制图片的按序播放,注意播放代码中的设置图片的大小是否更改的代码:
uiAnim.Snap = false;
这是按需求更改了NGUI的UISpriteAnimation.cs的脚本代码,为脚本中的mSnap变量添加了设置接口,可以比较原代码和更改后的UISpriteAnimation代码:
原UISpriteAnimation代码:
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//---------------------------------------------- using UnityEngine;
using System.Collections.Generic; /// <summary>
/// Very simple sprite animation. Attach to a sprite and specify a common prefix such as "idle" and it will cycle through them.
/// </summary> [ExecuteInEditMode]
[RequireComponent(typeof(UISprite))]
[AddComponentMenu("NGUI/UI/Sprite Animation")]
public class UISpriteAnimation : MonoBehaviour
{
[HideInInspector][SerializeField] protected int mFPS = ;
[HideInInspector][SerializeField] protected string mPrefix = "";
[HideInInspector][SerializeField] protected bool mLoop = true;
[HideInInspector][SerializeField] protected bool mSnap = true; protected UISprite mSprite;
protected float mDelta = 0f;
protected int mIndex = ;
protected bool mActive = true;
protected List<string> mSpriteNames = new List<string>(); /// <summary>
/// Number of frames in the animation.
/// </summary> public int frames { get { return mSpriteNames.Count; } } /// <summary>
/// Animation framerate.
/// </summary> public int framesPerSecond { get { return mFPS; } set { mFPS = value; } } /// <summary>
/// Set the name prefix used to filter sprites from the atlas.
/// </summary> public string namePrefix { get { return mPrefix; } set { if (mPrefix != value) { mPrefix = value; RebuildSpriteList(); } } } /// <summary>
/// Set the animation to be looping or not
/// </summary> public bool loop { get { return mLoop; } set { mLoop = value; } } /// <summary>
/// Returns is the animation is still playing or not
/// </summary> public bool isPlaying { get { return mActive; } } /// <summary>
/// Rebuild the sprite list first thing.
/// </summary> protected virtual void Start ()
{
RebuildSpriteList(); } /// <summary>
/// Advance the sprite animation process.
/// </summary> protected virtual void Update ()
{
if (mActive && mSpriteNames.Count > && Application.isPlaying && mFPS > 0f)
{
mDelta += RealTime.deltaTime;
float rate = 1f / mFPS; if (rate < mDelta)
{ mDelta = (rate > 0f) ? mDelta - rate : 0f;
if (++mIndex >= mSpriteNames.Count)
{
mIndex = ;
mActive = loop;
} if (mActive)
{
mSprite.spriteName = mSpriteNames[mIndex];
if (mSnap)
{
mSprite.MakePixelPerfect();
}
}
}
}
} /// <summary>
/// Rebuild the sprite list after changing the sprite name.
/// </summary> public void RebuildSpriteList ()
{
if (mSprite == null) mSprite = GetComponent<UISprite>();
mSpriteNames.Clear(); if (mSprite != null && mSprite.atlas != null)
{
List<UISpriteData> sprites = mSprite.atlas.spriteList; for (int i = , imax = sprites.Count; i < imax; ++i)
{
UISpriteData sprite = sprites[i]; if (string.IsNullOrEmpty(mPrefix) || sprite.name.StartsWith(mPrefix))
{
mSpriteNames.Add(sprite.name);
}
}
mSpriteNames.Sort();
}
} /// <summary>
/// Reset the animation to frame 0 and activate it.
/// </summary> public void Reset()
{
mActive = true;
mIndex = ; if (mSprite != null && mSpriteNames.Count > )
{
mSprite.spriteName = mSpriteNames[mIndex];
if (mSnap) mSprite.MakePixelPerfect();
}
}
}
更改后的UISpriteAnimation代码:
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//---------------------------------------------- using UnityEngine;
using System.Collections.Generic; /// <summary>
/// Very simple sprite animation. Attach to a sprite and specify a common prefix such as "idle" and it will cycle through them.
/// </summary> [ExecuteInEditMode]
[RequireComponent(typeof(UISprite))]
[AddComponentMenu("NGUI/UI/Sprite Animation")]
public class UISpriteAnimation : MonoBehaviour
{
[HideInInspector][SerializeField] protected int mFPS = ;
[HideInInspector][SerializeField] protected string mPrefix = "";
[HideInInspector][SerializeField] protected bool mLoop = true;
[HideInInspector][SerializeField] protected bool mSnap = true; protected UISprite mSprite;
protected float mDelta = 0f;
protected int mIndex = ;
protected bool mActive = true;
protected List<string> mSpriteNames = new List<string>(); /// <summary>
/// Number of frames in the animation.
/// </summary> public int frames { get { return mSpriteNames.Count; } } /// <summary>
/// Animation framerate.
/// </summary> public int framesPerSecond { get { return mFPS; } set { mFPS = value; } } /// <summary>
/// Set the name prefix used to filter sprites from the atlas.
/// </summary> public string namePrefix { get { return mPrefix; } set { if (mPrefix != value) { mPrefix = value; RebuildSpriteList(); } } } /// <summary>
/// Set the animation to be looping or not
/// </summary> public bool loop { get { return mLoop; } set { mLoop = value; } } /// <summary>
/// Returns is the animation is still playing or not
/// </summary> public bool isPlaying { get { return mActive; } } /// <summary>
/// Rebuild the sprite list first thing.
/// </summary> // 设置是否让图片显示原来大小还是按设置的大小进行缩放——vitah
public bool Snap
{
get
{
return this.mSnap;
}
set
{
this.mSnap = value;
}
} protected virtual void Start ()
{
RebuildSpriteList(); } /// <summary>
/// Advance the sprite animation process.
/// </summary> protected virtual void Update ()
{
if (mActive && mSpriteNames.Count > && Application.isPlaying && mFPS > 0f)
{
mDelta += RealTime.deltaTime;
float rate = 1f / mFPS; if (rate < mDelta)
{ mDelta = (rate > 0f) ? mDelta - rate : 0f;
if (++mIndex >= mSpriteNames.Count)
{
mIndex = ;
mActive = loop;
} if (mActive)
{
mSprite.spriteName = mSpriteNames[mIndex];
if (mSnap)
{
mSprite.MakePixelPerfect();
}
}
}
}
} /// <summary>
/// Rebuild the sprite list after changing the sprite name.
/// </summary> public void RebuildSpriteList ()
{
if (mSprite == null) mSprite = GetComponent<UISprite>();
mSpriteNames.Clear(); if (mSprite != null && mSprite.atlas != null)
{
List<UISpriteData> sprites = mSprite.atlas.spriteList; for (int i = , imax = sprites.Count; i < imax; ++i)
{
UISpriteData sprite = sprites[i]; if (string.IsNullOrEmpty(mPrefix) || sprite.name.StartsWith(mPrefix))
{
mSpriteNames.Add(sprite.name);
}
}
mSpriteNames.Sort();
}
} /// <summary>
/// Reset the animation to frame 0 and activate it.
/// </summary> public void Reset()
{
mActive = true;
mIndex = ; if (mSprite != null && mSpriteNames.Count > )
{
mSprite.spriteName = mSpriteNames[mIndex];
if (mSnap) mSprite.MakePixelPerfect();
}
}
}
新增的代码在63行位置,设置图片的大小是否更改的意思就是你导入的图片大小假定是600*100,但是你这时候sprite想显示的大小是300*100,假如不设置mSnap = false,NGUI会默认为true,这样每次播放动画的时候它会以图片的大小为显示大小,即最后显示在程序中的是600*100,设置mSnap = true;时,它就按你设定的大小进行缩放,最后显示的300*100的大小。
Unity NGUI实现序列帧动画播放的更多相关文章
- (转)NGUI系列教程七(序列帧动画UITexture 和 UIsprit)
NGUI系列教程七(序列帧动画) 今天我给大家讲一下如何使用NGUI做序列帧动画.本节主要包括两方面内容,分别是使用UIspirit和使用UITexture 做序列帧动画.废话不说了,下面开始.还 ...
- NGUI系列教程七(序列帧动画)
今天我给大家讲一下如何使用NGUI做序列帧动画.本节主要包括两方面内容,分别是使用UIspirit和使用UITexture 做序列帧动画.废话不说了,下面开始.还要在啰嗦一句,首先大家要准备一些序列帧 ...
- Unity NGUI实现按钮点击播放Aniamtion
unity版本:4.5 NGUI版本:3.6.5 参考链接:http://www.colabug.com/thread-1029974-1-1.html,作者:COLABUG.COM 橘虞 1.怎么创 ...
- Unity Shader序列帧动画学习笔记
Unity Shader序列帧动画学习笔记 关于无限播放序列帧动画的一点问题 在学shader的序列帧动画时,书上写了这样一段代码: fixed4 frag(v2f i){ // 获得整数时间 flo ...
- unity shader序列帧动画代码,顺便吐槽一下unity shader系统
一.看到UNITY论坛里有些人求unity shader序列帧动画,写shader我擅长啊,就顺势写了个CG的shader.代码很简单,就是变换UV采样序列帧贴图,美术配置行数列数以及变换速度. Sh ...
- Unity Shader 序列帧动画
shader中的序列帧动画属于纹理动画中的一种,主要原理是将给定的纹理进行等分,再根据时间的变化循环播放等分中的一部分. Unity Shader 内置时间变量 名称 类型 描述 _Time floa ...
- Unity NGUI 描点控件的位移动画
要让一个描点的控件动画移动到一个Position,能够用TweenPosition.可是这个仅仅能用在Position是固定的情况下.并且不能依据分辨率适配来进行移动. 以NGUI自带的 ...
- 【转】unity Animator 怎么判断一个动画播放结束
关于unity Animator 怎么判断一个动画播放结束这里有几种方法.希望对大家有帮助.还有其他办法的可以分享一下 第一种方法:在动画结束帧后面加个动画事件,调用下含这个变量的函数接口不是可以了? ...
- Cocos2d-x动画播放(序列帧)
简介 Cocos2d-x中,动画的具体内容是依靠精灵显示出来的,为了显示动态图片,我们需要不停切换精灵显示的内容,通过把静态的精灵变为动画播放器从而实现动画效果.动画由帧组成,每一帧都是一个纹理,我们 ...
随机推荐
- 提取DLL类库代码
@SET destFolder=.\bin@XCOPY /I /Y %SYSTEMDRIVE%\WINDOWS\assembly\GAC_MSIL\Microsoft.ReportViewer.Pro ...
- ZOJ 3903 Ant(公式推导)
这个公式推导过程是看的这位大牛的http://blog.csdn.net/bigbigship/article/details/49123643 扩展欧几里德求模的逆元方法: #include < ...
- CSS Hack (各个浏览器兼容的问题)
写css样式的时候,恐怕最头疼的就是各个浏览器下的兼容性问题,即css hack,明明感觉应该是对的,但是就是出不来效果,我根据平时所接触的,总结一下关于兼容 性的技巧,希望可以对大家有所帮助…… C ...
- wampserver 2.4 配置虚拟主机
最近用到了wamp环境,想创建一个虚拟主机,可是忘记了,于是百度了一下,把它写下来: 环境wampserver 2.4 找到安装目录,进入apache安装目录:找到conf 下的 httpd.conf ...
- 在vSphere5.0虚拟机里的Ubuntu Server 32位安装JDK
本机操作系统Win7 服务器用vSphere 5.0 虚拟机 在虚拟机安装了Ubuntu Server 12.04 1.首先到Oracle官网上下载jdk-7u51-linux-i586.tar.g ...
- Asp.net Mvc4 基于Authorize实现的模块访问权限
在MVC中,我们可以通过在action或者controller上设置Authorize[Role="xxx"] 的方式来设置用户对action的访问权限.显然,这样并不能满足我们的 ...
- What is SaaS?
SaaS, or Software as a Service, describes any cloud service where consumers are able to access softw ...
- [转载]hadoop SecondNamenode详解
SecondNamenode名字看起来很象是对第二个Namenode,要么与Namenode一样同时对外提供服务,要么相当于Namenode的HA.真正的了解了SecondNamenode以后,才发现 ...
- Composite 模式的实现
实现要点: 1.组合模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器. 2 ...
- 【随记】解决:VS2010 调试器无法继续继续运行该进程,无法启动调试
今天在调试项目的时候突然出现错误: 按照网上的一些方法弄了后还是同样报错,把本地代码删除后从库上重现拉下来的项目依然报错,到这里就明白不是项目本身问题了,而是VS2010 的问题,经过网上查资料,问同 ...