TaskMgr.cs 975 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using com.workman.cm3d2.task;
  5. using UnityEngine;
  6. public class TaskMgr : MonoBehaviour
  7. {
  8. public void SetTimeUntilStart(float timeUntilStart)
  9. {
  10. this.timeUntilStart = timeUntilStart;
  11. }
  12. public void StartTask(Action onFinished = null)
  13. {
  14. this.coroutine = this.DoTask(onFinished);
  15. base.StartCoroutine(this.coroutine);
  16. }
  17. private IEnumerator DoTask(Action onFinished = null)
  18. {
  19. yield return new WaitForSeconds(this.timeUntilStart);
  20. foreach (Task task in this.listTask)
  21. {
  22. task.Execute();
  23. yield return new WaitForSeconds(task.interval);
  24. }
  25. if (onFinished != null)
  26. {
  27. onFinished();
  28. }
  29. this.Clear();
  30. yield break;
  31. }
  32. private void Clear()
  33. {
  34. this.coroutine = null;
  35. }
  36. public void Stop()
  37. {
  38. if (this.coroutine != null)
  39. {
  40. base.StopCoroutine(this.coroutine);
  41. }
  42. }
  43. public List<Task> listTask;
  44. private IEnumerator coroutine;
  45. private float timeUntilStart;
  46. }