在指定的位置创建子物体,通过设定参数达到技能播放的效果

    1. 序列帧动画(文件格式:精灵图集文件) ```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;

    1. public bool IsThreadPlay = false;
    2. public ThreadEnum IsThreadName = 0;
    3. private float played_time;
    4. private bool is_playing = false;
    5. // 当前针的图片
    6. private Image img;
    7. private bool currentDelay = false;
    8. private bool currentPlayDelay = false;
    9. private int loopCount = 0;
    10. void Start()
    11. {
    12. this.img = this.GetComponent<Image>();
    13. img.color = new Color(255, 255, 255, 0f);
    14. if (this.play_onload)
    15. {
    16. this.Play();
    17. }
    18. }
    19. /// <summary>
    20. /// 播放帧动画
    21. /// </summary>
    22. public void Play()
    23. {
    24. if (this.is_loop)
    25. {
    26. this.play_loop();
    27. }
    28. else
    29. {
    30. this.play_once();
    31. }
    32. }
    33. void play_once()
    34. {
    35. if (this.sprite_frame.Length <= 1)
    36. {
    37. return;
    38. }
    39. if (this.timeDelay > 0)
    40. {
    41. this.currentDelay = true;
    42. Invoke("UpdateTimeDelayState", this.timeDelay);
    43. }
    44. this.played_time = 0;
    45. this.is_playing = true;
    46. this.is_loop = false;
    47. }
    48. /// <summary>
    49. /// 循环播放帧动画
    50. /// </summary>
    51. void play_loop()
    52. {
    53. if (this.sprite_frame.Length <= 1)
    54. {
    55. return;
    56. }
    57. if (this.timeDelay > 0)
    58. {
    59. this.currentDelay = true;
    60. Invoke("UpdateTimeDelayState", this.timeDelay);
    61. }
    62. this.played_time = 0;
    63. this.is_playing = true;
    64. this.is_loop = true;
    65. }
    66. /// <summary>
    67. /// 停止播放帧动画
    68. /// </summary>
    69. public void Stop()
    70. {
    71. this.is_playing = false;
    72. }
    73. /// <summary>
    74. /// 延迟播放帧动画
    75. /// </summary>
    76. private void UpdateTimeDelayState()
    77. {
    78. currentDelay = false;
    79. }
    80. private void UpdatePlayTimeDelayState()
    81. {
    82. currentPlayDelay = false;
    83. }
    84. void Update()
    85. {
    86. if (!this.is_playing)
    87. {
    88. return;
    89. }
    90. //如果延迟时间没有到, 直接返回
    91. if (this.currentDelay || this.currentPlayDelay)
    92. {
    93. return;
    94. }
    95. if (ColorAlpha <= 1)
    96. {
    97. ColorAlpha += Time.deltaTime / 2; //透明度渐变效果
    98. }
    99. img.color = new Color(255, 255, 255, ColorAlpha);
    100. float dt = Time.deltaTime;
    101. this.played_time += dt;
    102. int index = (int)(this.played_time / this.duration);
    103. if (playType < 2)
    104. {
    105. if (!this.is_loop)
    106. {
    107. if (index >= this.sprite_frame.Length)
    108. {
    109. if (countindex == 0 && this.sprite_frame1 != null)
    110. {
    111. this.played_time -= (this.duration * sprite_frame.Length);
    112. index -= sprite_frame.Length;
    113. this.sprite_frame = this.sprite_frame1;
    114. index = 0;
    115. countindex++;
    116. }
    117. else if (countindex == 1 && this.sprite_frame2 != null)
    118. {
    119. this.played_time -= (this.duration * sprite_frame.Length);
    120. index -= sprite_frame.Length;
    121. this.sprite_frame = this.sprite_frame2;
    122. index = 0;
    123. countindex++;
    124. }
    125. else
    126. {
    127. if (IsThreadPlay && IsThreadName != 0)
    128. {
    129. FightManager.Instance().ChangeThreadStateResume(IsThreadName);
    130. }
    131. IsThreadPlay = false;
    132. this.is_playing = false;
    133. this.played_time = 0;
    134. if (this.isDestroy)
    135. {
    136. Destroy(this.gameObject); //单次循环后删除该物体
    137. }
    138. }
    139. }
    140. else
    141. {
    142. this.img.sprite = sprite_frame[index];
    143. this.img.SetNativeSize();//重置图片大小
    144. }
    145. }
    146. else
    147. {
    148. while (index >= this.sprite_frame.Length)
    149. {
    150. loopCount++;
    151. this.played_time -= (this.duration * sprite_frame.Length);
    152. index -= sprite_frame.Length;
    153. this.currentPlayDelay = true;
    154. Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
    155. }
    156. if (loopCount < LoopNumber || LoopNumber == 0)
    157. {
    158. this.img.sprite = sprite_frame[index];
    159. this.img.SetNativeSize();//重置图片大小
    160. }
    161. else
    162. {
    163. Destroy(this.gameObject); //循环条件不满足后删除该物体
    164. }
    165. }
    166. }
    167. else
    168. {
    169. if (this.sprite_frame_Start != null)
    170. {
    171. if (index >= this.sprite_frame_Start.Length)
    172. {
    173. this.sprite_frame_Start = null;
    174. this.played_time = 0;
    175. index = 0;
    176. }
    177. else
    178. {
    179. this.img.sprite = sprite_frame_Start[index];
    180. this.img.SetNativeSize();//重置图片大小
    181. }
    182. }
    183. else
    184. {
    185. if (this.sprite_frame != null)
    186. {
    187. while (index >= this.sprite_frame.Length)
    188. {
    189. loopCount++;
    190. this.played_time -= (this.duration * sprite_frame.Length);
    191. index -= sprite_frame.Length;
    192. this.currentPlayDelay = true;
    193. Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
    194. }
    195. if (loopCount < LoopNumber || LoopNumber == 0)
    196. {
    197. this.img.sprite = sprite_frame[index];
    198. this.img.SetNativeSize();//重置图片大小
    199. }
    200. else
    201. {
    202. this.sprite_frame = null;
    203. this.played_time = 0;
    204. index = 0;
    205. }
    206. }
    207. else
    208. {
    209. if (this.sprite_frame_End != null)
    210. {
    211. if (index >= this.sprite_frame_End.Length)
    212. {
    213. if (IsThreadPlay && IsThreadName != 0)
    214. {
    215. FightManager.Instance().ChangeThreadStateResume(IsThreadName);
    216. }
    217. IsThreadPlay = false;
    218. this.is_playing = false;
    219. this.played_time = 0;
    220. if (this.isDestroy)
    221. {
    222. Destroy(this.gameObject); //单次循环后删除该物体
    223. }
    224. }
    225. else
    226. {
    227. this.img.sprite = sprite_frame_End[index];
    228. this.img.SetNativeSize();//重置图片大小
    229. }
    230. }
    231. }
    232. }
    233. }
    234. }

    }

    1. 2. 序列帧图片(文件格式:Png图片)
    2. ```csharp
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. [RequireComponent(typeof(RawImage))]
    6. public class ImageAnimationManage : MonoBehaviour
    7. {
    8. //帧动画文件
    9. public Texture2D[] TextureFrames;
    10. public Texture2D[] TextureFrames1;
    11. public Texture2D[] TextureFrames2;
    12. public Texture2D[] TextureFrames_Start;
    13. public Texture2D[] TextureFrames_End;
    14. // 帧动画播放的间隔时间
    15. public float duration = 0.1f;
    16. // 是否循环播放
    17. public bool is_loop = false;
    18. // 循环播放几次
    19. public int LoopNumber = 0;
    20. // 是否在加载的时候播放
    21. public bool play_onload = true;
    22. // 播放完成后动画是否销毁
    23. public bool isDestroy = false;
    24. // 延迟多少秒播放动画
    25. public float timeDelay = 0;
    26. // 如果是重复播放动画多少秒后在播放
    27. public float playTimeEndDelay = 0;
    28. //图片透明程度
    29. public float ColorAlpha = 2f;
    30. //多个文件循环条件
    31. public int countindex = 2;
    32. //循环类型(单次播放=0,循环播放=1,带有开始动画化和结尾动画的循环播放=2)
    33. public int playType = 0;
    34. private float played_time;
    35. private bool is_playing = false;
    36. // 当前针的图片
    37. private RawImage img;
    38. private bool currentDelay = false;
    39. private bool currentPlayDelay = false;
    40. private int loopCount = 0;
    41. void Start()
    42. {
    43. this.img = this.GetComponent<RawImage>();
    44. img.color = new Color(255, 255, 255, 0f);
    45. if (this.play_onload)
    46. {
    47. this.Play();
    48. }
    49. }
    50. /// <summary>
    51. /// 播放帧动画
    52. /// </summary>
    53. public void Play()
    54. {
    55. if (this.is_loop)
    56. {
    57. this.play_loop();
    58. }
    59. else
    60. {
    61. this.play_once();
    62. }
    63. }
    64. void play_once()
    65. {
    66. if (this.TextureFrames.Length <= 1)
    67. {
    68. return;
    69. }
    70. if (this.timeDelay > 0)
    71. {
    72. this.currentDelay = true;
    73. Invoke("UpdateTimeDelayState", this.timeDelay);
    74. }
    75. this.played_time = 0;
    76. this.is_playing = true;
    77. this.is_loop = false;
    78. }
    79. /// <summary>
    80. /// 循环播放帧动画
    81. /// </summary>
    82. void play_loop()
    83. {
    84. if (this.TextureFrames.Length <= 1)
    85. {
    86. return;
    87. }
    88. if (this.timeDelay > 0)
    89. {
    90. this.currentDelay = true;
    91. Invoke("UpdateTimeDelayState", this.timeDelay);
    92. }
    93. this.played_time = 0;
    94. this.is_playing = true;
    95. this.is_loop = true;
    96. }
    97. /// <summary>
    98. /// 停止播放帧动画
    99. /// </summary>
    100. public void Stop()
    101. {
    102. this.is_playing = false;
    103. }
    104. /// <summary>
    105. /// 延迟播放帧动画
    106. /// </summary>
    107. private void UpdateTimeDelayState()
    108. {
    109. currentDelay = false;
    110. }
    111. private void UpdatePlayTimeDelayState()
    112. {
    113. currentPlayDelay = false;
    114. }
    115. void Update()
    116. {
    117. if (!this.is_playing)
    118. {
    119. return;
    120. }
    121. //如果延迟时间没有到, 直接返回
    122. if (this.currentDelay || this.currentPlayDelay)
    123. {
    124. return;
    125. }
    126. if (ColorAlpha <= 1)
    127. {
    128. ColorAlpha += Time.deltaTime / 2; //透明度渐变效果
    129. }
    130. img.color = new Color(255, 255, 255, ColorAlpha);
    131. float dt = Time.deltaTime;
    132. this.played_time += dt;
    133. int index = (int)(this.played_time / this.duration);
    134. if (playType < 2)
    135. {
    136. if (!this.is_loop)
    137. {
    138. if (index >= this.TextureFrames.Length)
    139. {
    140. if (countindex == 0 && this.TextureFrames1 != null)
    141. {
    142. this.played_time -= (this.duration * TextureFrames.Length);
    143. index -= TextureFrames.Length;
    144. this.TextureFrames = this.TextureFrames1;
    145. index = 0;
    146. countindex++;
    147. }
    148. else if (countindex == 1 && this.TextureFrames2 != null)
    149. {
    150. this.played_time -= (this.duration * TextureFrames.Length);
    151. index -= TextureFrames.Length;
    152. this.TextureFrames = this.TextureFrames2;
    153. index = 0;
    154. countindex++;
    155. }
    156. else
    157. {
    158. this.is_playing = false;
    159. this.played_time = 0;
    160. if (this.isDestroy)
    161. {
    162. Destroy(this.gameObject); //单次循环后删除该物体
    163. }
    164. }
    165. }
    166. else
    167. {
    168. this.img.texture = TextureFrames[index];
    169. this.img.SetNativeSize();//重置图片大小
    170. }
    171. }
    172. else
    173. {
    174. while (index >= this.TextureFrames.Length)
    175. {
    176. loopCount++;
    177. this.played_time -= (this.duration * TextureFrames.Length);
    178. index -= TextureFrames.Length;
    179. this.currentPlayDelay = true;
    180. Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
    181. }
    182. if (loopCount < LoopNumber || LoopNumber == 0)
    183. {
    184. this.img.texture = TextureFrames[index];
    185. this.img.SetNativeSize();//重置图片大小
    186. }
    187. else
    188. {
    189. Destroy(this.gameObject); //循环条件不满足后删除该物体
    190. }
    191. }
    192. }
    193. else
    194. {
    195. if (this.TextureFrames_Start != null)
    196. {
    197. if (index >= this.TextureFrames_Start.Length)
    198. {
    199. this.TextureFrames_Start = null;
    200. this.played_time = 0;
    201. index = 0;
    202. }
    203. else
    204. {
    205. this.img.texture = TextureFrames_Start[index];
    206. this.img.SetNativeSize();//重置图片大小
    207. }
    208. }
    209. else
    210. {
    211. if (this.TextureFrames != null)
    212. {
    213. while (index >= this.TextureFrames.Length)
    214. {
    215. loopCount++;
    216. this.played_time -= (this.duration * TextureFrames.Length);
    217. index -= TextureFrames.Length;
    218. this.currentPlayDelay = true;
    219. Invoke("UpdatePlayTimeDelayState", this.playTimeEndDelay);
    220. }
    221. if (loopCount < LoopNumber || LoopNumber == 0)
    222. {
    223. this.img.texture = TextureFrames[index];
    224. this.img.SetNativeSize();//重置图片大小
    225. }
    226. else
    227. {
    228. this.TextureFrames = null;
    229. this.played_time = 0;
    230. index = 0;
    231. }
    232. }
    233. else
    234. {
    235. if (this.TextureFrames_End != null)
    236. {
    237. if (index >= this.TextureFrames_End.Length)
    238. {
    239. this.is_playing = false;
    240. this.played_time = 0;
    241. if (this.isDestroy)
    242. {
    243. Destroy(this.gameObject); //单次循环后删除该物体
    244. }
    245. }
    246. else
    247. {
    248. this.img.texture = TextureFrames_End[index];
    249. this.img.SetNativeSize();//重置图片大小
    250. }
    251. }
    252. }
    253. }
    254. }
    255. }
    256. }