签到领奖

[图文教程] ugui image动画,支持动画完成通知

[复制链接]
查看: 981|回复: 0
5武林高手
1449/2000
排名
162
昨日变化
2
发表于 2016-12-13 11:28:04 | 显示全部楼层 |阅读模式

1.png


锤子打下的动画播完之后,锤子和字要消失,牌子要播裂开动画。虽然简单,但是写死的话很蛋疼,所以造了一个通用的轮子。

2.png


3.png


4.png


这样就完成了通知回调。
完整源代码如下:
[AppleScript] 纯文本查看 复制代码
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];
}
}


文/Babybus_Unity
原文链接:http://www.jianshu.com/p/6cf5e14b4da4



+1
981°C
沙发哦 ^ ^ 马上
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /2 下一条

在线客服(工作时间:9:00-22:00)
010-82609395
泰斗社区公众号

Copyright   ©2015-2016  【泰斗社区】-专注互联网游戏和应用的开发者平台  Powered by©Discuz!  技术支持:迪恩网络     ( 沪ICP备14023207号-9 )