Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- namespace Test
- {
- public class TestMonoBehaviour : MonoBehaviour
- {
- public readonly string Name;
- public int Count => _actions.Count;
- private readonly Queue<QueueAction> _actions = new Queue<QueueAction>();
- public IEnumerator Execute()
- {
- var prepare = ColorUtility.ToHtmlStringRGB(Color.magenta);
- var go = ColorUtility.ToHtmlStringRGB(Color.yellow);
- Debug.Log($"<color=#{prepare}>PREPARE....</color>");
- yield return new WaitForSeconds(2f);
- Debug.Log($"<color=#{go}>GO!!!</color>");
- var sequence = StartCoroutine(_actions.Where(action => action.IsParallel == false)
- .Select(action => action.Execute()).GetEnumerator());
- var parallel = StartCoroutine(_actions.Where(action => action.IsParallel == true)
- .Select(action => StartCoroutine(action.Execute())).ToList().GetEnumerator());
- yield return sequence;
- yield return parallel;
- }
- private void Awake()
- {
- for (int i = 0; i < 4; i++)
- {
- _actions.Enqueue(new QueueAction($"{nameof(Method)}{i}") {LongAction = Method(3, $"I'm parallel{i}, YES", Color.green), IsParallel = true});
- _actions.Enqueue(new QueueAction($"{nameof(Method)}{i}") {LongAction = Method(3, $"I'm NOT parallel{i}", Color.red), IsParallel = false});
- }
- }
- private IEnumerator Start()
- {
- Debug.Log("<color=#FFFF00>Start</color>");
- yield return StartCoroutine(Execute());
- Debug.Log("<color=#FFFF00>Done</color>");
- }
- private IEnumerator Method(float sec, string message, Color color)
- {
- var name = ColorUtility.ToHtmlStringRGB(color);
- var start = ColorUtility.ToHtmlStringRGB(Color.magenta);
- var end = ColorUtility.ToHtmlStringRGB(Color.cyan);
- Debug.Log($"<color=#{name}>{message}</color> - <color=#{start}>START</color>");
- yield return new WaitForSeconds(sec);
- Debug.Log($"<color=#{name}>{message}</color> - <color=#{end}>END</color>");
- }
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [Serializable]
- public class QueueAction
- {
- public readonly string Name;
- public Action FastAction;
- public IEnumerator LongAction;
- public bool IsParallel = false;
- public QueueAction(string name)
- {
- Debug.Log($"Create action {name}");
- Name = name;
- }
- public IEnumerator Execute()
- {
- Debug.Log($"Start action {Name}");
- if (FastAction != null)
- {
- FastAction.Invoke();
- }
- if (LongAction != null)
- {
- yield return LongAction;
- }
- Debug.Log($"End action {Name}");
- yield return null;
- }
- public QueueAction Clone()
- {
- return new QueueAction(Name)
- {
- FastAction = FastAction,
- LongAction = LongAction,
- IsParallel = IsParallel,
- };
- }
- }
- public class CoroutineQueue
- {
- public readonly string Name;
- public int Count => _actions.Count;
- private readonly Queue<QueueAction> _actions = new Queue<QueueAction>();
- public CoroutineQueue(string name)
- {
- Debug.Log($"Create queue {name}");
- Name = name;
- }
- private void AppendAction(QueueAction action)
- {
- Debug.Log($"Queue {Name}. Append action {action.Name}");
- _actions.Enqueue(action);
- }
- // public void AppendAction(string name, Action fastAction, IEnumerator longAction = null) =>
- // AppendAction(new QueueAction(name){
- // FastAction = fastAction,
- // LongAction = longAction,
- // });
- public void AppendAction(string name, Action fastAction, bool isParallel = false) =>
- AppendAction(new QueueAction(name)
- {
- FastAction = fastAction,
- IsParallel = isParallel,
- });
- public void AppendAction(string name, IEnumerator longAction, bool isParallel = false) =>
- AppendAction(new QueueAction(name)
- {
- LongAction = longAction,
- IsParallel = isParallel,
- });
- public void AppendQueue(CoroutineQueue queue, bool isParallel = false)
- {
- Debug.Log($"Queue {Name}. Append queue {queue.Name}");
- bool isFirst = true;
- foreach (QueueAction queueAction in queue._actions)
- {
- if (isFirst)
- {
- isFirst = false;
- QueueAction firstAction = queueAction.Clone();
- firstAction.IsParallel = isParallel;
- _actions.Enqueue(firstAction);
- }
- else
- {
- _actions.Enqueue(queueAction);
- }
- }
- }
- public IEnumerator Execute()
- {
- // Debug.Log($"Start queue {Name}");
- while (_actions.Count > 0)
- {
- List<QueueAction> parallelActions = new List<QueueAction>();
- do
- {
- parallelActions.Add(_actions.Dequeue());
- } while (_actions.Count > 0 && _actions.Peek().IsParallel);
- if (parallelActions.Count > 0)
- {
- if (parallelActions.Count == 1)
- {
- yield return parallelActions[0].Execute();
- }
- else
- {
- Debug.Log($"Queue {Name}. Start parallel actions");
- List<IEnumerator> parallels = new List<IEnumerator>();
- foreach (QueueAction parallelAction in parallelActions)
- {
- // Debug.Log($"Queue {Name}. Start action {parallelAction.Name}");
- parallels.Add(parallelAction.Execute());
- // Debug.Log($"Queue {Name}. End action {parallelAction.Name}");
- }
- foreach (IEnumerator parallel in parallels)
- {
- yield return parallel; //ВЫПОЛНЯЕТСЯ ПОСЛЕДОВАТЕЛЬНО !!!!!!!!!!!
- }
- Debug.Log($"Queue {Name}. End parallel actions");
- }
- }
- }
- yield return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement