1
\$\begingroup\$

I'm using google admob to show rewarded video ads in my game. Once the game is over, the play video button is enabled. If the user clicks on that, a panel opens asking "would you like to revive the player by watching an ad". There are two options, one is to hit "ok" and the other to cancel "x". The issue is that the I have to click on "ok" multiple times for the ad to show. Sometimes the ad doesn't show at all no matter how many times I click on "ok" .Here is the code I have in adscript:

public class AdScript : MonoBehaviour
{

string App_ID = "ca-app-pub-123454545~12345678";

string Video_Ad_Id = "ca-app-pub-3940256099942544/5224354917";

private RewardedAd rewardedAd;

void Start()
{
    MobileAds.Initialize(App_ID);        
}

public void RequestRewardBasedVideo()
{
    
    this.rewardedAd = new RewardedAd(Video_Ad_Id);
    AdRequest request = new AdRequest.Builder().Build();
    
    this.rewardedAd.LoadAd(request);
    this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
    
    this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
    
    this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
    
    this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
   
    this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
   
    this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;
}

public void ShowVideoRewardAd()
{
    if (this.rewardedAd.IsLoaded())
    {
        this.rewardedAd.Show();
    }
}

public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleRewardedAdLoaded event received");
}

public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
{
    MonoBehaviour.print(
        "HandleRewardedAdFailedToLoad event received with message: "
                         + args.Message);
}

public void HandleRewardedAdOpening(object sender, EventArgs args)
{
    MonoBehaviour.print("HandleRewardedAdOpening event received");
}

public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
{
    MonoBehaviour.print(
        "HandleRewardedAdFailedToShow event received with message: "
                         + args.Message);
}

public void HandleRewardedAdClosed(object sender, EventArgs args)
{
    this.RequestRewardBasedVideo();
}

public void HandleUserEarnedReward(object sender, Reward args)
 {
    GameManager.instance.ReceiveReward();
 }
}

The function RequestRewardBasedVideo() is added to the play video button and the ShowVideoRewardAd() is added to the "ok" button. The test ads show properly but the issue is that I have to hit the ok button multiple times for the ad to display. I'm not sure why this is happening. Can someone please help?

\$\endgroup\$
0

2 Answers 2

0
\$\begingroup\$

Ads can take awhile to load. Like, a long while. So long, in fact, that I usually start loading my first ad on app launch, and start loading the next ad immediately after the previous ad is finished playing.

My guess is that this:

public void ShowVideoRewardAd()
{
    if (this.rewardedAd.IsLoaded())
    {
        this.rewardedAd.Show();
    }
}

is just skipping over the call to Show over and over every time you tap the button, until the ad is actually loaded.

My suggestion is to not even show the option to watch an ad unless IsLoaded returns true. Alternatively, you can pop up a "please wait" dialog when the user taps to watch the video if IsLoaded is false, and wait for either HandleRewardedAdLoaded or HandleRewardedAdFailedToLoad is called.

\$\endgroup\$
3
  • \$\begingroup\$ thanks a lot for the help...loading the ad on app launch is showing the ad everytime....one more question is there any way to load two different types of rewards like one for reviving the player and the other for adding 100 points to the score...I looked at the documentation, there isin't much information...in this function "public void HandleUserEarnedReward(object sender, Reward args) { GameManager.instance.ReceiveReward(); }", I can only have one reward....how do I add a second one like GameManager.instance.AddtoScore(); \$\endgroup\$ Commented Aug 28, 2020 at 8:50
  • \$\begingroup\$ @singlearrowgames I'd suggest making a new post for that, as the answer is too large to fit into the margins. \$\endgroup\$
    – Ed Marty
    Commented Aug 28, 2020 at 17:23
  • \$\begingroup\$ I have created the new post - gamedev.stackexchange.com/questions/185637/… \$\endgroup\$ Commented Sep 8, 2020 at 11:12
0
\$\begingroup\$

I'll take a wild guess here (two guesses, actually).

The first guess is that the Ad framework is not yet completely initialized once you try to load your first ad. Based on the documentation, it seems you could pass a callback to the MobileAds.Initialize(); that would be called once the initialization is done. You would need to wait until the initialization is properly completed before requesting your first ads. At first sight, the documentation is pretty bad regarding how to do this exactly, so I suggest you dig a bit further to find more about how you could achieve this.

The second guess is that you're trying to load an ad before you set the event handlers. If we take another look at the documentation, it appears that they place the calls to

// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
this.rewardedAd.LoadAd(request);

after they have set up the event listeners. The guess is that as soon as you ask it to "LoadAd", the framework fetches the list of listeners at that moment, and go do asynchronous stuff with it and calls the methods you had supplied at the moment of the call. There was no listeners at the moment of the call to LoadAd, so perhaps this is why you see "nothing happening" and you need to "tap multiple times", because on the first try, there is no listeners, and the listeners would be there only on the second time.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .