I am trying to make a Queue of methods which start executing next method once previous method get excuted completely, that queue of methods is working fine for all scenario's except IEnumerator return type one.
Following are list of methods that i am trying to execute one after another.
public void CallMethod()
{
UnityMainThreadDispatcher.Schedule(() =>
{
ShowMessage("Minimum!");
}
, 0.3f);
Debug.Log("Winner of this round is " + winnerPlayer.GetName());
UnityMainThreadDispatcher.Schedule(() =>
{
scoreboardPopup.ShowPopup();
}
, 5.0f);
UnityMainThreadDispatcher.Schedule(() =>
{
CardDistributionAnimation.instance.PlayCardDistributionAnimation(false);
}, 0.5f);
UnityMainThreadDispatcher.Schedule(() => StartNextRound(),0.5f);
Debug.Log("Call Minimum Completed");
}
UnityMainThread Class
public class UnityMainThreadDispatcher : MonoBehaviour
{
private Queue<IEnumerator> coroutineQueue = new Queue<IEnumerator>();
public delegate void Task();
public static void Schedule(Task task, float delay)
{
Debug.Log("Task Scheduled " + task.Method);
//Instance().Enqueue(task, delay);
Instance().coroutineQueue.Enqueue(DoTask(task, delay));
}
private static IEnumerator DoTask(Task task, float delay)
{
Debug.Log("Inside DoTask Coroutine");
yield return new WaitForSeconds(delay);
task();
}
IEnumerator CoroutineCoordinator()
{
while (true)
{
while (coroutineQueue.Count > 0)
yield return StartCoroutine(coroutineQueue.Dequeue());
yield return null;
}
}
private static UnityMainThreadDispatcher _instance = null;
public static bool Exists()
{
return _instance != null;
}
public static UnityMainThreadDispatcher Instance()
{
if (!Exists())
{
throw new Exception("UnityMainThreadDispatcher could not find the UnityMainThreadDispatcher object. Please ensure you have added the MainThreadExecutor Prefab to your scene.");
}
return _instance;
}
private void Start()
{
StartCoroutine(CoroutineCoordinator());
}
}
CoroutineQueue will only start next method in its queue, once previous method got executed succussfully, It is working correctly for all except for below method
CardDistributionAnimation.instance.PlayCardDistributionAnimation(false);
It is happening because PlayCardDistributionAnimation executes on Coroutine method.
PlayCardDistributionAnimation method
public void PlayCardDistributionAnimation(bool isNewGame)
{
generateCards(isNewGame);
StartCoroutine(DistributeCardsToPlayer());
}
public void generateCards(bool isNewGame)
{
int size = 0;
if (generatedCards.Count > 0)
generatedCards.Clear();
GameObject distributionobject = GameObject.Find("CardDistributionObject");
if (isNewGame)
size = playersPosition.Count * 2;
else
size = playersPosition.Count;
for (int i = 0; i < size; i++)
{
GameObject vector2 = Instantiate(cardsBack, distributionobject.transform);
generatedCards.Add(vector2);
vector2.SetActive(true);
}
}
private IEnumerator DistributeCardsToPlayer()
{
Debug.Log("Inside Distribute Cards To Player");
for(int i=0;i< generatedCards.Count();i++)
{
var cover = Instantiate(cardsBack, generatedCards[i].transform.position, Quaternion.identity, generatedCards[i].transform);
cover.GetComponent<RectTransform>().localScale = Vector3.one;
var tween = cover.transform.DOMove(playersPosition[i%(playersPosition.Count)].transform.position, 0.5f);
tween.OnComplete(() => Destroy(cover));
yield return new WaitForSeconds(0.6f);
}
yield return new WaitForSeconds(1f);
//playersPosition.First().SetActive(true);
foreach (GameObject gameObject in generatedCards)
Destroy(gameObject);
isCardDistributionCompleted = true;
Debug.Log("Card Distribution method executed succussfully");
}
I am not sure how i cannot stop other methods to execute, till my Coroutine gets executed completely. I have tried few approaches but none have worked so far, any suggestion would be really helpful.