CoroutineWrapper.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.ComponentModel;
  5. /// <summary>
  6. /// 封装协程,提供便捷使用方式
  7. /// </summary>
  8. public sealed class CoroutineWrapper : MonoSingleton<CoroutineWrapper>
  9. {
  10. /// <summary>
  11. /// 延迟n帧执行
  12. /// </summary>
  13. /// <param name="frames"></param>
  14. /// <param name="ev"></param>
  15. /// <returns></returns>
  16. IEnumerator ExeDelay(int frames, Action ev)
  17. {
  18. for (int i = 0; i < frames; i++)
  19. {
  20. yield return new WaitForEndOfFrame();
  21. }
  22. ev();
  23. }
  24. /// <summary>
  25. /// 延迟n秒执行
  26. /// </summary>
  27. /// <param name="sec"></param>
  28. /// <param name="ev"></param>
  29. /// <returns></returns>
  30. IEnumerator ExeDelayS(float sec, Action ev)
  31. {
  32. yield return new WaitForSeconds(sec);
  33. ev();
  34. }
  35. /// <summary>
  36. /// 延迟n秒后执行
  37. /// </summary>
  38. /// <param name="sec"></param>
  39. /// <param name="ev"></param>
  40. public void EXEDelaySecs(float sec, Action ev)
  41. {
  42. StartCoroutine(ExeDelayS(sec, ev));
  43. }
  44. /// <summary>
  45. /// 延迟n帧后执行
  46. /// </summary>
  47. /// <param name="frames"></param>
  48. /// <param name="ev"></param>
  49. public void EXEDelayFrames(int frames, Action ev)
  50. {
  51. StartCoroutine(ExeDelay(frames, ev));
  52. }
  53. /// <summary>
  54. /// 每帧通知
  55. /// </summary>
  56. public event Action OnPerFrame;
  57. void Update()
  58. {
  59. OnPerFrame?.Invoke();
  60. }
  61. }