0
\$\begingroup\$

I'm implementing reward based video ads in my game using Google Admob. There are two functions that provide the reward. One is a ReceiveLife() function where once the player dies, if they click on the revive button the game restarts and the score is set to score before the player dies instead of 0. The other function is ReceivePoints(), where if the player clicks on the add points button, they are rewarded with 100 extra points. I looked at the documentation provided in google admob regarding loading multiple rewarded ads, but I'm unsure of how to go about calling these two functions in the "HandleRewardedAdClosed" function. Here is the code that works for only function (ReceiveLife()):

public class AdScript : MonoBehaviour
{
    string Video_Ad_Id = "ca-app-pub-3940256099942544/5224354917";    
    private RewardedAd rewardedAd;   
    
    void Start()
    {       
        MobileAds.Initialize(initStatus => { });
        RequestRewardBasedVideo();    
    }
    
    public void RequestRewardBasedVideo()
    {        
        this.rewardedAd = new RewardedAd(Video_Ad_Id);        
        this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;       
        this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;
        // Other event hooks...
        AdRequest request = new AdRequest.Builder().Build();      
        this.rewardedAd.LoadAd(request);
    }

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

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

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

How do I call two rewarded functions in the HandleUserEarnedReward() function above. Can someone please help?

\$\endgroup\$
1
\$\begingroup\$

Your example has a single RewardedAd instance. You'll need two if you want to follow Google's documentation. Then when you have two, you can assign different callbacks to each. Something like this:

public class AdScript : MonoBehaviour
{
string Video_Ad_Id = "ca-app-pub-3940256099942544/5224354917";    
private RewardedAd extraLifeRewardedAd;
private RewardedAd receivePointsRewardedAd;

void Start()
{       
    MobileAds.Initialize(initStatus => { });
    extraLifeRewardedAd = RequestRewardBasedVideo(ReceiveExtraLife);
    receivePointsRewardedAd = RequestRewardBasedVideo(ReceivePoints);
}

public void RequestRewardBasedVideo(EventHandler<Reward> rewardCallback)
{
    RewardedAd rewardedAd = new RewardedAd(Video_Ad_Id);        
    rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;       
    rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;       
    rewardedAd.OnAdOpening += HandleRewardedAdOpening;       
    rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;       
    rewardedAd.OnUserEarnedReward += rewardCallback;       
    rewardedAd.OnAdClosed += HandleRewardedAdClosed;
    RequestAd(rewardedAd);
    return rewardedAd;
}

private void RequestAd(RewardedAd rewardedAd) {
    AdRequest request = new AdRequest.Builder().Build();      
    rewardedAd.LoadAd(request);
}

public void ShowReceiveLifeVideoRewardAd()
{
    if (this.extraLifeRewardedAd.IsLoaded())
    {
        this.extraLifeRewardedAd.Show();            
    }
}

public void ShowExtraPointsVideoRewardAd()
{
    if (this.receivePointsRewardedAd.IsLoaded())
    {
        this.receivePointsRewardedAd.Show();            
    }
}
.
.
.
.
public void HandleRewardedAdClosed(object sender, EventArgs args)
{
    RequestAd((RewardBasedVideoAd)sender);
}

public void ReceiveExtraLife(object sender, Reward args)
{
    GameManager.instance.ReceiveLife();
}

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

An alternative would be to have to different instances of this script, since it is a MonoBehaviour, and use a UnityEvent to dispatch the callback to the correct target, but that's getting a bit more complicated. It would be easily extensible to have anything happen upon receiving a reward, but since you only have these two things, this is probably sufficient.

\$\endgroup\$
1
  • \$\begingroup\$ thanks for the help....I will implement this method...if it's not too much to ask, can you please help me out with another question that I had posted regarding ads....here the link - gamedev.stackexchange.com/questions/185606/…, once again thanks for all your help... \$\endgroup\$ – single arrow games Sep 8 '20 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.