public CoroutineTask CreateAndAddSubCoroutineTask(IEnumerator coroutine)
        {
            var j = new CoroutineTask(coroutine, false);

            AddSubCoroutineTask(j);
            return(j);
        }
 public void AddSubCoroutineTask(CoroutineTask subCoroutineTask)
 {
     if (gSubCoroutineTaskStack == null)
     {
         gSubCoroutineTaskStack = new Stack <CoroutineTask>();
     }
     gSubCoroutineTaskStack.Push(subCoroutineTask);
 }
        private IEnumerator ExecuteSubTask()
        {
            // Check if there is any sub-coroutines in the stack.
            if (gSubCoroutineTaskStack != null && gSubCoroutineTaskStack.Count > 0)
            {
                do
                {
                    // Get first sub-task.
                    CoroutineTask subTask = gSubCoroutineTaskStack.Pop();

                    // Execute the sub task.
                    yield return(CoroutineCoordinator.Instance.StartCoroutine(subTask.StartAsCoroutine()));
                }
                // Keep executing the sub tasks as long as there is any.
                while(gSubCoroutineTaskStack.Count > 0);
            }
        }
        public void RemoveSubCoroutineTask(CoroutineTask subCoroutineTask)
        {
            if (gSubCoroutineTaskStack.Contains(subCoroutineTask))
            {
                var subCoroutineTaskStack = new Stack <CoroutineTask>(gSubCoroutineTaskStack.Count - 1);
                var allCurrentSubTasks    = gSubCoroutineTaskStack.ToArray();
                System.Array.Reverse(allCurrentSubTasks);

                for (var i = 0; i < allCurrentSubTasks.Length; i++)
                {
                    var j = allCurrentSubTasks[i];
                    if (j != subCoroutineTask)
                    {
                        subCoroutineTaskStack.Push(j);
                    }
                }

                // assign the new stack
                gSubCoroutineTaskStack = subCoroutineTaskStack;
            }
        }