How to properly wait for Unity Ad to be watched before continuing game logic?
Hi everyone.
In my game user has a single life. When he dies, I display a menu where he can select to restart the game or watch a Unity ad to continue the game from the point where he died. He can do this only once. If he watches a Unity ad, then continues game and dies again, he is not given the option to watch another one, he has to restart the game. There is a very simple end menu where the user can select to "restart" or "watch ad". If user selects "watch ad", I simply set the boolean wannaWatchAd to true and return to menuScript which is the attached script for the startMenu.
In the very start wannaWatchAd is false, so the first time the script is called, the startMenu is displayed in the else where the Debug.Log with string starting with ">>> 5". When the user dies and selects to watch an ad, wannaWatchAd is set to true and the code goes into blocks marked with Debug.Log entries ">>> 1", then ">>> 2". At that point as I am yielding return from the function ShowRewardedAd(), I expect the code to wait until the co-routine ShowRewardedAd() finishes and continue with the code block marked with ">>> 3" but the code goes right into the block marked with ">>> 4". The result is, user watches the video but can not continue the game and faces the startMenu.
What I see in the debug output is as follows:
I/Unity ( 8790): >>> 5 In if WannaWatchAd is false, so we open menu False 0
I/Unity ( 8790): >>> 1 Start of MenuScript.Start, WannaWatchAd = True 17.96005
I/Unity ( 8790): >>> 2 In if WannaWatchAd is true, so we show ad and yield its return True 17.96005
I/Unity ( 8790): >>> 4 Ad is NOT watched, AdWatched = False WannaWatchAd = True 17.96005
I/Unity ( 8790): >>> 3 Ad is watched so AdWatched = False WannaWatchAd = False 18.08006
Check the time displayed at the end of lines. Code goes into blocks 2 and 4 but at the same time it is yielding return of the co-routine and once the co-routine ends, it goes to block 3 although AdWatched is false at that time. That is the second thing that puzzles me about how things work in there.
I based my code on the example code snippets given with Unity Ads but I guess they are designed to give the player some reward asynchronously. When I try to actually wait for the ad before continuing with the game logic, they do not work. So, any ideas how I could approach this particular problem? Is it even possible to stop code execution and wait for Unity ad to be watched and then continue?
Thanks in advance.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Advertisements;
public class MenuScript : MonoBehaviour {
public GameObject birdie;
public GameObject startMenu;
private IEnumerator Start()
{
Debug.Log(">>> 1 Start of MenuScript.Start, WannaWatchAd = " + GameManager.WannaWatchAd + " " + Time.time);
if (GameManager.WannaWatchAd)
{
Debug.Log(">>> 2 In if WannaWatchAd is true, so we show ad and yield its return " + GameManager.WannaWatchAd + " " + Time.time);
GameManager.AdWatched = false;
yield return StartCoroutine(ShowRewardedAd());
if (GameManager.AdWatched)
{
Debug.Log(">>> 3 Ad is watched so AdWatched = " + GameManager.AdWatched + " WannaWatchAd = " + GameManager.WannaWatchAd + " " + Time.time);
birdie.GetComponent<BirdController>().ReLive();
}
else
{
Debug.Log(">>> 4 Ad is NOT watched, AdWatched = " + GameManager.AdWatched + " WannaWatchAd = " + GameManager.WannaWatchAd + " " + Time.time);
GameManager.coins = 0;
startMenu.SetActive(true);
}
GameManager.WannaWatchAd = false;
}
else
{
Debug.Log(">>> 5 In if WannaWatchAd is false, so we open menu " + GameManager.WannaWatchAd + " " + Time.time);
GameManager.coins = 0;
startMenu.SetActive(true);
}
yield return null;
}
private IEnumerator ShowRewardedAd()
{
#if UNITY_ANDROID || UNITY_IOS
if (Advertisement.IsReady("rewardedVideo"))
{
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show("rewardedVideo", options);
}
#endif
yield return null;
}
#if UNITY_ANDROID || UNITY_IOS
private void HandleShowResult(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
GameManager.AdWatched = true;
Debug.Log("The ad was successfully shown.");
break;
case ShowResult.Skipped:
Debug.Log("The ad was skipped before reaching the end.");
break;
case ShowResult.Failed:
Debug.LogError("The ad failed to be shown.");
break;
}
}
#endif