在指定的位置创建子物体,通过设定参数达到技能播放的效果
- 序列帧动画(文件格式:精灵图集文件) ```csharp using UnityEngine; using UnityEngine.UI; using System.Threading;
//自动添加图片组件 [RequireComponent(typeof(Image))] public class FrameAnimationManage : MonoBehaviour { //帧动画文件 public Sprite[] sprite_frame; public Sprite[] sprite_frame1; public Sprite[] sprite_frame2; public Sprite[] sprite_frame_Start; public Sprite[] sprite_frame_End; // 帧动画播放的间隔时间 public float duration = 0.1f; // 是否循环播放 public bool is_loop = false; // 循环播放几次 public int LoopNumber = 0; // 是否在加载的时候播放 public bool play_onload = true; // 播放完成后动画是否销毁 public bool isDestroy = false; // 延迟多少秒播放动画 public float timeDelay = 0; // 如果是重复播放动画多少秒后在播放 public float playTimeEndDelay = 0; //图片透明程度 public float ColorAlpha = 2f; //多个文件循环条件 public int countindex = 2; //循环类型(单次播放=0,循环播放=1,带有开始动画化和结尾动画的循环播放=2) public int playType = 0;
public bool IsThreadPlay = false;
public ThreadEnum IsThreadName = 0;
private float played_time;
private bool is_playing = false;
// 当前针的图片
private Image img;
private bool currentDelay = false;
private bool currentPlayDelay = false;
private int loopCount = 0;
void Start()
{
this.img = this.GetComponent<Image>();
img.color = new Color(255, 255, 255, 0f);
if (this.play_onload)
{
this.Play();
}
}
/// <summary>
/// 播放帧动画
/// </summary>
public void Play()
{
if (this.is_loop)
{
this.play_loop();
}
else
{
this.play_once();
}
}
void play_once()
{
if (this.sprite_frame.Length <= 1)
{
return;
}
if (this.timeDelay > 0)
{
this.currentDelay = true;
Invoke("UpdateTimeDelayState", this.timeDelay);
}
this.played_time = 0;
this.is_playing = true;
this.is_loop = false;
}
/// <summary>
/// 循环播放帧动画
/// </summary>
void play_loop()
{
if (this.sprite_frame.Length <= 1)
{
return;
}
if (this.timeDelay > 0)
{
this.currentDelay = true;
Invoke("UpdateTimeDelayState", this.timeDelay);
}
this.played_time = 0;
this.is_playing = true;
this.is_loop = true;
}
/// <summary>
/// 停止播放帧动画
/// </summary>
public void Stop()
{
this.is_playing = false;
}
/// <summary>
/// 延迟播放帧动画
/// </summary>
private void UpdateTimeDelayState()
{
currentDelay = false;
}
private void UpdatePlayTimeDelayState()
{
currentPlayDelay = false;
}
void Update()
{
if (!this.is_playing)
{
return;
}
//如果延迟时间没有到, 直接返回
if (this.currentDelay || this.currentPlayDelay)
{
return;
}
if (ColorAlpha <= 1)
{
ColorAlpha += Time.deltaTime / 2; //透明度渐变效果
}
img.color = new Color(255, 255, 255, ColorAlpha);
float dt = Time.deltaTime;
this.played_time += dt;
int index = (int)(this.played_time / this.duration);
if (playType < 2)
{
if (!this.is_loop)
{
if (index >= this.sprite_frame.Length)
{
if (countindex == 0 && this.sprite_frame1 != null)
{
this.played_time -= (this.duration * sprite_frame.Length);
index -= sprite_frame.Length;
this.sprite_frame = this.sprite_frame1;
index = 0;
countindex++;
}
else if (countindex == 1 && this.sprite_frame2 != null)
{
this.played_time -= (this.duration * sprite_frame.Length);
index -= sprite_frame.Length;
this.sprite_frame = this.sprite_frame2;
index = 0;
countindex++;
}
else
{
if (IsThreadPlay && IsThreadName != 0)
{
FightManager.Instance().ChangeThreadStateResume(IsThreadName);
}
IsThreadPlay = false;
this.is_playing = false;
this.played_time = 0;
if (this.isDestroy)
{
Destroy(this.gameObject); //单次循环后删除该物体
}
}
}
else
{
this.img.sprite = sprite_frame[index];
this.img.SetNativeSize();//重置图片大小
}
}
else
{
while (index >= this.sprite_frame.Length)
{
loopCount++;
this.played_time -= (this.duration * sprite_frame.Length);
index -= sprite_frame.Length;
this.currentPlayDelay = true;
Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
}
if (loopCount < LoopNumber || LoopNumber == 0)
{
this.img.sprite = sprite_frame[index];
this.img.SetNativeSize();//重置图片大小
}
else
{
Destroy(this.gameObject); //循环条件不满足后删除该物体
}
}
}
else
{
if (this.sprite_frame_Start != null)
{
if (index >= this.sprite_frame_Start.Length)
{
this.sprite_frame_Start = null;
this.played_time = 0;
index = 0;
}
else
{
this.img.sprite = sprite_frame_Start[index];
this.img.SetNativeSize();//重置图片大小
}
}
else
{
if (this.sprite_frame != null)
{
while (index >= this.sprite_frame.Length)
{
loopCount++;
this.played_time -= (this.duration * sprite_frame.Length);
index -= sprite_frame.Length;
this.currentPlayDelay = true;
Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
}
if (loopCount < LoopNumber || LoopNumber == 0)
{
this.img.sprite = sprite_frame[index];
this.img.SetNativeSize();//重置图片大小
}
else
{
this.sprite_frame = null;
this.played_time = 0;
index = 0;
}
}
else
{
if (this.sprite_frame_End != null)
{
if (index >= this.sprite_frame_End.Length)
{
if (IsThreadPlay && IsThreadName != 0)
{
FightManager.Instance().ChangeThreadStateResume(IsThreadName);
}
IsThreadPlay = false;
this.is_playing = false;
this.played_time = 0;
if (this.isDestroy)
{
Destroy(this.gameObject); //单次循环后删除该物体
}
}
else
{
this.img.sprite = sprite_frame_End[index];
this.img.SetNativeSize();//重置图片大小
}
}
}
}
}
}
}
2. 序列帧图片(文件格式:Png图片)
```csharp
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(RawImage))]
public class ImageAnimationManage : MonoBehaviour
{
//帧动画文件
public Texture2D[] TextureFrames;
public Texture2D[] TextureFrames1;
public Texture2D[] TextureFrames2;
public Texture2D[] TextureFrames_Start;
public Texture2D[] TextureFrames_End;
// 帧动画播放的间隔时间
public float duration = 0.1f;
// 是否循环播放
public bool is_loop = false;
// 循环播放几次
public int LoopNumber = 0;
// 是否在加载的时候播放
public bool play_onload = true;
// 播放完成后动画是否销毁
public bool isDestroy = false;
// 延迟多少秒播放动画
public float timeDelay = 0;
// 如果是重复播放动画多少秒后在播放
public float playTimeEndDelay = 0;
//图片透明程度
public float ColorAlpha = 2f;
//多个文件循环条件
public int countindex = 2;
//循环类型(单次播放=0,循环播放=1,带有开始动画化和结尾动画的循环播放=2)
public int playType = 0;
private float played_time;
private bool is_playing = false;
// 当前针的图片
private RawImage img;
private bool currentDelay = false;
private bool currentPlayDelay = false;
private int loopCount = 0;
void Start()
{
this.img = this.GetComponent<RawImage>();
img.color = new Color(255, 255, 255, 0f);
if (this.play_onload)
{
this.Play();
}
}
/// <summary>
/// 播放帧动画
/// </summary>
public void Play()
{
if (this.is_loop)
{
this.play_loop();
}
else
{
this.play_once();
}
}
void play_once()
{
if (this.TextureFrames.Length <= 1)
{
return;
}
if (this.timeDelay > 0)
{
this.currentDelay = true;
Invoke("UpdateTimeDelayState", this.timeDelay);
}
this.played_time = 0;
this.is_playing = true;
this.is_loop = false;
}
/// <summary>
/// 循环播放帧动画
/// </summary>
void play_loop()
{
if (this.TextureFrames.Length <= 1)
{
return;
}
if (this.timeDelay > 0)
{
this.currentDelay = true;
Invoke("UpdateTimeDelayState", this.timeDelay);
}
this.played_time = 0;
this.is_playing = true;
this.is_loop = true;
}
/// <summary>
/// 停止播放帧动画
/// </summary>
public void Stop()
{
this.is_playing = false;
}
/// <summary>
/// 延迟播放帧动画
/// </summary>
private void UpdateTimeDelayState()
{
currentDelay = false;
}
private void UpdatePlayTimeDelayState()
{
currentPlayDelay = false;
}
void Update()
{
if (!this.is_playing)
{
return;
}
//如果延迟时间没有到, 直接返回
if (this.currentDelay || this.currentPlayDelay)
{
return;
}
if (ColorAlpha <= 1)
{
ColorAlpha += Time.deltaTime / 2; //透明度渐变效果
}
img.color = new Color(255, 255, 255, ColorAlpha);
float dt = Time.deltaTime;
this.played_time += dt;
int index = (int)(this.played_time / this.duration);
if (playType < 2)
{
if (!this.is_loop)
{
if (index >= this.TextureFrames.Length)
{
if (countindex == 0 && this.TextureFrames1 != null)
{
this.played_time -= (this.duration * TextureFrames.Length);
index -= TextureFrames.Length;
this.TextureFrames = this.TextureFrames1;
index = 0;
countindex++;
}
else if (countindex == 1 && this.TextureFrames2 != null)
{
this.played_time -= (this.duration * TextureFrames.Length);
index -= TextureFrames.Length;
this.TextureFrames = this.TextureFrames2;
index = 0;
countindex++;
}
else
{
this.is_playing = false;
this.played_time = 0;
if (this.isDestroy)
{
Destroy(this.gameObject); //单次循环后删除该物体
}
}
}
else
{
this.img.texture = TextureFrames[index];
this.img.SetNativeSize();//重置图片大小
}
}
else
{
while (index >= this.TextureFrames.Length)
{
loopCount++;
this.played_time -= (this.duration * TextureFrames.Length);
index -= TextureFrames.Length;
this.currentPlayDelay = true;
Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
}
if (loopCount < LoopNumber || LoopNumber == 0)
{
this.img.texture = TextureFrames[index];
this.img.SetNativeSize();//重置图片大小
}
else
{
Destroy(this.gameObject); //循环条件不满足后删除该物体
}
}
}
else
{
if (this.TextureFrames_Start != null)
{
if (index >= this.TextureFrames_Start.Length)
{
this.TextureFrames_Start = null;
this.played_time = 0;
index = 0;
}
else
{
this.img.texture = TextureFrames_Start[index];
this.img.SetNativeSize();//重置图片大小
}
}
else
{
if (this.TextureFrames != null)
{
while (index >= this.TextureFrames.Length)
{
loopCount++;
this.played_time -= (this.duration * TextureFrames.Length);
index -= TextureFrames.Length;
this.currentPlayDelay = true;
Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
}
if (loopCount < LoopNumber || LoopNumber == 0)
{
this.img.texture = TextureFrames[index];
this.img.SetNativeSize();//重置图片大小
}
else
{
this.TextureFrames = null;
this.played_time = 0;
index = 0;
}
}
else
{
if (this.TextureFrames_End != null)
{
if (index >= this.TextureFrames_End.Length)
{
this.is_playing = false;
this.played_time = 0;
if (this.isDestroy)
{
Destroy(this.gameObject); //单次循环后删除该物体
}
}
else
{
this.img.texture = TextureFrames_End[index];
this.img.SetNativeSize();//重置图片大小
}
}
}
}
}
}
}