如题,要实现序列帧的播放导入图片的时候需要注意:

  (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实现序列帧动画播放的更多相关文章

  1. (转)NGUI系列教程七(序列帧动画UITexture 和 UIsprit)

    NGUI系列教程七(序列帧动画)   今天我给大家讲一下如何使用NGUI做序列帧动画.本节主要包括两方面内容,分别是使用UIspirit和使用UITexture 做序列帧动画.废话不说了,下面开始.还 ...

  2. NGUI系列教程七(序列帧动画)

    今天我给大家讲一下如何使用NGUI做序列帧动画.本节主要包括两方面内容,分别是使用UIspirit和使用UITexture 做序列帧动画.废话不说了,下面开始.还要在啰嗦一句,首先大家要准备一些序列帧 ...

  3. Unity NGUI实现按钮点击播放Aniamtion

    unity版本:4.5 NGUI版本:3.6.5 参考链接:http://www.colabug.com/thread-1029974-1-1.html,作者:COLABUG.COM 橘虞 1.怎么创 ...

  4. Unity Shader序列帧动画学习笔记

    Unity Shader序列帧动画学习笔记 关于无限播放序列帧动画的一点问题 在学shader的序列帧动画时,书上写了这样一段代码: fixed4 frag(v2f i){ // 获得整数时间 flo ...

  5. unity shader序列帧动画代码,顺便吐槽一下unity shader系统

    一.看到UNITY论坛里有些人求unity shader序列帧动画,写shader我擅长啊,就顺势写了个CG的shader.代码很简单,就是变换UV采样序列帧贴图,美术配置行数列数以及变换速度. Sh ...

  6. Unity Shader 序列帧动画

    shader中的序列帧动画属于纹理动画中的一种,主要原理是将给定的纹理进行等分,再根据时间的变化循环播放等分中的一部分. Unity Shader 内置时间变量 名称 类型 描述 _Time floa ...

  7. Unity NGUI 描点控件的位移动画

           要让一个描点的控件动画移动到一个Position,能够用TweenPosition.可是这个仅仅能用在Position是固定的情况下.并且不能依据分辨率适配来进行移动. 以NGUI自带的 ...

  8. 【转】unity Animator 怎么判断一个动画播放结束

    关于unity Animator 怎么判断一个动画播放结束这里有几种方法.希望对大家有帮助.还有其他办法的可以分享一下 第一种方法:在动画结束帧后面加个动画事件,调用下含这个变量的函数接口不是可以了? ...

  9. Cocos2d-x动画播放(序列帧)

    简介 Cocos2d-x中,动画的具体内容是依靠精灵显示出来的,为了显示动态图片,我们需要不停切换精灵显示的内容,通过把静态的精灵变为动画播放器从而实现动画效果.动画由帧组成,每一帧都是一个纹理,我们 ...

随机推荐

  1. ios中的界面跳转方式

    ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...

  2. 10.7 noip模拟试题

    楼[问题背景]zhx 为他的妹子造了一幢摩天楼.[问题描述]zhx 有一幢摩天楼. 摩天楼上面有 M 个观光电梯,每个观光电梯被两个整数

  3. (转载)记录函数 getStyle() 获取元素 CSS 样式

    设置元素(element)的css属性值可以用element的style属性,例如要将element的背景色设置为黑色,可以这么做: element.style.backgroundColor = ' ...

  4. EntityFramework在root目录web.config中的配置设置

    未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...

  5. 函数对象的prototype总结

    通过看 http://www.cnblogs.com/mindsbook/archive/2009/09/19/javascriptYouMustKnowPrototype.html 该文章和对代码的 ...

  6. c# 双问号运算

    model.id??0 ??运算:如果运算符左边的值为NULL侧返回右边的值,否则返回左边的值

  7. 拦截器getmodel方法什么时候被调用(没搞懂有什么鸟用,自己搭的项目中用到了这个)

    拦截器是Struts2最强大的特性之一,它是一种可以让用户在Action执行之前和Result执行之后进行一些功能处理的机制.Struts2 的预定义拦截器 modelDriven 如果action实 ...

  8. 写个接口的实现类,在方法的前面加了@Override居然报错

    据说这是jdk的问题,@Override是JDK5就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现 ...

  9. c++面试(一)

    1.在c++中可以通过"::"来直接操作全局变量. 2.i++与++i效率的比较. (1)內建数据类型时,他们的效率差别不大. (2)自定义数据类型(类等)的情况,(++i)可以返 ...

  10. OpenCV2.4.9 & Visual Studio 2010 环境配置篇

    1. 准备工作 1.1. 安装 Visual Studio 2010, 需要安装 VC++ 相关功能.具体可求助度娘. 1.2. 下载 OpenCV 2.4.9 For Windows:https:/ ...