Babybus-u3d技术交流-ugui image动画,支持动画完成通知。(改自ngui)
ugui image动画,支持动画完成通知。(改自ngui)
锤子打下的动画播完之后,锤子和字要消失,牌子要播裂开动画。虽然简单,但是写死的话很蛋疼,所以造了一个通用的轮子。
这样就完成了通知回调。
完整源代码如下:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
/// <summary>
/// Small script that makes it easy to create looping 2D sprite animations.
/// </summary>
public class ImageAnimation : MonoBehaviour
{
/// <summary>
/// How many frames there are in the animation per second.
/// </summary>
[SerializeField]
protected int framerate = 20;
/// <summary>
/// Should this animation be affected by time scale?
/// </summary>
public bool ignoreTimeScale = true;
/// <summary>
/// Should this animation be looped?
/// </summary>
public bool loop = true;
/// <summary>
/// Actual sprites used for the animation.
/// </summary>
public UnityEngine.Sprite[] frames;
Image mImage;
int mIndex = 0;
float mUpdate = 0f;
public UnityEvent onFinished;
/// <summary>
/// Returns is the animation is still playing or not
/// </summary>
public bool isPlaying
{
get
{
return enabled;
}
}
/// <summary>
/// Animation framerate.
/// </summary>
public int framesPerSecond
{
get
{
return framerate;
}
set
{
framerate = value;
}
}
/// <summary>
/// Continue playing the animation. If the animation has reached the end, it will restart from beginning
/// </summary>
public void Play ()
{ if (frames != null && frames.Length > 0)
{
if (!enabled && !loop)
{
int newIndex = framerate > 0 ? mIndex + 1 : mIndex - 1;
if (newIndex < 0 || newIndex >= frames.Length)
mIndex = framerate < 0 ? frames.Length - 1 : 0;
}
enabled = true;
UpdateSprite();
}
}
/// <summary>
/// Pause the animation
/// </summary>
public void Pause ()
{
enabled = false;
}
/// <summary>
/// Reset the animation to the beginning.
/// </summary>
public
void
ResetToBeginning ()
{
mIndex = framerate < 0 ? frames.Length - 1 : 0;
UpdateSprite();
}
/// <summary>
/// Start playing the animation right away.
/// </summary>
void Start ()
{
Play();
}
/// <summary>
/// Advance the animation as necessary.
/// </summary>
void Update ()
{
![Uploading Paste_Image_927161.png . . .]
if
(frames ==
null
|| frames.Length == 0)
{
enabled = false;
}
else if (framerate != 0)
{
float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
if (mUpdate < time)
{
mUpdate = time;
int newIndex = framerate > 0 ? mIndex + 1 : mIndex - 1;
if (!loop && (newIndex < 0 || newIndex >= frames.Length))
{
onFinished.Invoke();
enabled = false;
return;
}
mIndex = RepeatIndex(newIndex, frames.Length):
UpdateSprite();
}
}
}
static public int RepeatIndex(int val, int max)
{
if (max < 1)
return 0;
while (val < 0)
val += max;
while(val >= max) val -= max;
return val;
}
/// <summary>
/// Immediately update the visible sprite.
/// </summary>
void UpdateSprite ()
{
if (mImage == null)
{
mImage = GetComponent<Image>();
if (mImage == null)
{
enabled =false;
return;
}
}
float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
if (framerate != 0) mUpdate = time + Mathf.Abs(1f / framerate);
if (mImage != null)
mImage.sprite = frames[mIndex];
}
}