简体   繁体   中英

Unity 3D: Admob rewarded Ads event callback fires only once

I have Admob rewarded ads in my game and I have an continue button as soon as player die, so the problem is that when I click on continue the ads shows and the player revives(callback fired) but say if player died again (same run) and click to continue the ad would show but no callback fires and for the callback to fire I have to reset the scene. The ads shows all the time but callback fires only once per scene.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using GoogleMobileAds.Api;
using GoogleMobileAds;
using UnityEngine.UI;

public class AdsManager:MonoBehaviour

{
    public GameObject PlayerScript;
public static AdsManager AdsInstance;



string Rewarded_AD = "ca-app-pub-3940256099942544/5224354917"; // these all 3 ids are test ids, swap them later with real ids, delete this line when done
string Inter_AD = "ca-app-pub-3940256099942544/1033173712";

private RewardedAd rewardedAD;
// private RewardBasedVideoAd rewardedAD;
private InterstitialAd InterAd;

private void Awake()
{
    if (AdsInstance == null)
    {
        AdsInstance = this;
    }
    else if (AdsInstance != this)
    {
        Destroy(gameObject);
    }




}

private void Start() {
    MobileAds.Initialize(initStatus => {

        
        Debug.Log("Initialize done");
       


    });

    PlayerScript = GameObject.FindGameObjectWithTag("players");



    this.RequestRewardedAd();
    this.RequestInterAd();


    this.rewardedAD.OnAdClosed += HandleRewardedAdCloseed;
    this.rewardedAD.OnUserEarnedReward += HandleUserEarnedRewards;

   
}
private void Update()
{
    PlayerScript = GameObject.FindGameObjectWithTag("players");

   
}


public void RequestRewardedAd()
{
    //  this.rewardedAD =  RewardBasedVideoAd.Instance;   //RewardBasedVideoAds(Deprecated ones)

    this.rewardedAD = new RewardedAd(Rewarded_AD);

    AdRequest request = new AdRequest.Builder().Build();
  //  this.rewardedAD.LoadAd(request, Rewarded_AD);     //RewardBasedVideoAds(Deprecated ones)
    this.rewardedAD.LoadAd(request);
}


public void ShowRewardBasedVideoAds()
{
    if (rewardedAD.IsLoaded())
    {
        rewardedAD.Show();
        
    }
}


public void RequestInterAd()
{
    this.InterAd = new InterstitialAd(Inter_AD);


    AdRequest request = new AdRequest.Builder().Build();

    this.InterAd.LoadAd(request);

}
public void ShowInterstitialAd()
{
    if (InterAd.IsLoaded())
    {

        InterAd.Show();
        ScoreManagerScript.ScoreInstance.AddCoinsOnWatchingAd();




        RequestInterAd();
    }

}


public void HandleRewardedAdCloseed(object sender, EventArgs args)
{
   
    MonoBehaviour.print("HandleRewardedAdClosed event received");
    this.RequestRewardedAd();
    PlayerScript.GetComponent<Player>().rb.isKinematic = true;
    PlayerScript.GetComponent<Player>().StartEnemySpikeTimer();
    // ScoreManagerScript.ScoreInstance.ReAddTheCoinsIfAdIsClosed();

}


public void HandleUserEarnedRewards(object sender, Reward args)
{

  
    MonoBehaviour.print("HandleRewardedAd event received ");

    PlayerScript.GetComponent<Player>().ReSpawnPlayer();

    ScoreManagerScript.ScoreInstance.ReduceCoinsOnContinue();

    this.RequestRewardedAd();
    
}

Also I have tried swapping the ads subscription into the RequestRewardedAd() method so yeah it doesn't work either, actually when I do that then only one ad is shown per scene and only one callback.

Please help thanks

I think I discovered what might be wrong, please give it a test and tell me if it works, I can't test it right now.

Try adding the following function:

void InitializeRewardedAd()
{
    //Creating a new instance of rewarded ads
    this.rewardedAD = new RewardedAd(Rewarded_AD);

    //loading the ads
    AdRequest request = new AdRequest.Builder().Build();

    this.rewardedAD.LoadAd(request);

    //subscribing the events to that new instance
    this.rewardedAD.OnAdClosed += HandleRewardedAdCloseed;
    this.rewardedAD.OnUserEarnedReward += HandleUserEarnedRewards;
}

and call InitializeRewardedAds() when you close the rewarded ads HandleRewardedAdCloseed (you only need to call it once when you close it, you are currently calling it both when you close it and when it fires a reward)

I think the problem can be that the events are not being subscribed to the new instance of the ad, only at the start, so only when you restart the scene it gets subscribed again.

PS: Just one other thing, do you really need to search for "PlayerScript" every frame? It can affect your performance. Maybe it would be nicer to do it once at Start() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM