ugui image动画,支持动画完成通知。(改自ngui)

Babybus-u3d技术交流-ugui image动画,支持动画完成通知。(改自ngui)

ugui image动画,支持动画完成通知。(改自ngui)

ugui image动画,支持动画完成通知。(改自ngui)_第1张图片
1.png

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

ugui image动画,支持动画完成通知。(改自ngui)_第2张图片
2.png
ugui image动画,支持动画完成通知。(改自ngui)_第3张图片
3.png

ugui image动画,支持动画完成通知。(改自ngui)_第4张图片
4.png

这样就完成了通知回调。

完整源代码如下:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
/// 
/// Small script that makes it easy to create looping 2D sprite animations.
/// 
 
public class ImageAnimation : MonoBehaviour
{
/// 
 
/// How many frames there are in the animation per second.
  /// 
 
[SerializeField] 
protected int framerate = 20;
 
/// 
  
/// Should this animation be affected by time scale?
    
/// 
   
public bool ignoreTimeScale = true;
   
/// 
  
/// Should this animation be looped?
    
/// 
   
public bool loop = true;
 
/// 
   
/// Actual sprites used for the animation.
   
/// 
   
public UnityEngine.Sprite[] frames;
  
Image mImage;
   
int mIndex = 0;
  
float mUpdate = 0f;

  
public UnityEvent onFinished;
  
/// 
    
/// Returns is the animation is still playing or not
   
/// 

    
public bool isPlaying    
{        
get        
{            
return enabled;        
}
    
}
    
///     
/// Animation framerate.
 /// 
    
public int framesPerSecond    
{        
get        
{            
return framerate;        
}        
set        
{            
framerate = value;        
}   
}
    
///     
/// Continue playing the animation. If the animation has reached the end, it will restart from beginning
 /// 
    
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();        
}
    
}

/// 
/// Pause the animation    
///      
public void Pause ()    
{        
enabled = false;    
}
/// 

/// Reset the animation to the beginning.

/// 

    
public
 void
 ResetToBeginning ()

    
{

        
mIndex = framerate < 0 ? frames.Length - 1 : 0;
UpdateSprite();
}     
/// 
/// Start playing the animation right away.
/// 
void Start ()    
{
      Play();    
}    
///  
/// Advance the animation as necessary.    
/// 
    
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;
}
   
/// 
/// Immediately update the visible sprite.
/// 
    void UpdateSprite ()
{
if (mImage == null)        
{
mImage = GetComponent();             
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];
}
}

你可能感兴趣的:(ugui image动画,支持动画完成通知。(改自ngui))