Unity NGUI实现序列帧动画播放

时间:2021-12-10 05:20:49

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

  (1)图片的命名要连续,如图:

  Unity NGUI实现序列帧动画播放

  (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的大小。