Coroutine stop executing when switch threads to show advertisment
Prerequisits:
application with button
AdMob advertisment plugin
cool down timer for button implemented in a coroutine
ability to watch advertisment to skip cool down as a reward
Expacted:
When the button is cooling down and you click it you are able to watch rewarded video advertisment to skip the cool down. If you have successfully watched the video the cool down is skipped. If you have closed the video before the reward callback was fired the cool down just have to keep going. And here is the problem.
Actual:
The cool down coroutine just stops. As I can see during debugging there is still a pointer to the coroutine, but it doesn't running.
My thoughts
As we know AdMob client shows advertisment in its own thread different from Unity Main thread. And as I understand, the Main thred is suspended during the ads thread is running. And when execution returns to the Main thread the coroutine should keep running. And here's some questions:
Does Main thread really susspended when advertisment is showing
Why do we need threads if it is going to be susspended?
Is there something I don't know about debugging multithreading app?
Here part of my code to give more info:
I change the pause state right before ads started. And when I close the adverisment the pause state became false, so coroutine can keep running further.
private void GameEvent(GameManager.GameEvent @event, GameManager.GameEvent previousEvent)
{
switch (@event)
{
case GameManager.GameEvent.Pause:
_pause = true;
break;
case GameManager.GameEvent.Overlay:
_pause = true;
break;
case GameManager.GameEvent.Play:
_pause = false;
break;
}
}
Here is a Timer coroutine that accept time and callback
private IEnumerator TimerWithAction(float time, Action action)
{
var seconds = time;
while (_pause)
{
yield return null;
}
while (seconds > 0)
{
yield return null;
while (_pause)
{
yield return null;
}
seconds -= Time.deltaTime;
_coolDownRoutineTime = seconds;
}
action();
}
As you can see the coroutine should wait while the pause state is true. And I thought that the Main thread going to execute this code concurrently with Advertisment thread. But no.
I'm not looking for solution, I am searching for the answers and knowledge. I would be glad If you can share your knowledge and explain what is going on.