How to check if an advertisement has finished in U

2019-09-10 07:03发布

问题:

I am using a rewarded video ad from admob in unity. The advertise is called when the player dies and asks them if they want to watch an ad to be revived. I am trying to check when the advertised has finished so I can reward the player.

void Update()
{
    if(isCalled == true){
        string adUnitId = "ca-app-pub-5920324855307233/4458481507";
        RewardBasedVideoAd rewardBasedVideo = RewardBasedVideoAd.Instance;
        AdRequest request = new AdRequest.Builder().Build();
        rewardBasedVideo.LoadAd(request, adUnitId);
        showAd(rewardBasedVideo);
    }
}

public void showAd(RewardBasedVideoAd rewardBasedVideo)
{
    if (rewardBasedVideo.IsLoaded())
    {
        //Subscribe to Ad event
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        rewardBasedVideo.Show();
    }
}

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    string type = args.Type;
    double amount = args.Amount;
    //Reawrd User here
    print("User rewarded with: " + amount.ToString() + " " + type);

    isCalled = false;

    managerScript.revival();
    managerScript.Loading.SetActive(false);
}

The first time the advertised is watched in the scene the method revival is called and the loading screen is closed and everything works fine, after that when the advertise is watched again it does not work, the "HandleRewardBasedVideoRewarded" method is not called , a new advertise just pops up to watch over and over. How do i fix it, since the game is not calling the "HandleRewardBasedVideoRewarded" method when the ads finish the second time. so the method revival is called and the loading screen is closed like it does on the first time the game is opened. Note: The issue still happens even when the scene is restarted (not the entire game).

Update: I have edited the script to your solution but the problem is still occuring. Whay isnt it working? Here is the full script:

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

public class GameAdvertising : MonoBehaviour {

    public bool isCalled;
    public GameObject Manager;
    GameManager managerScript;

    string adUnitId = "ca-app-pub-5920324855307233/4458481507";
    RewardBasedVideoAd rewardBasedVideo = null;

    void Start () {
        managerScript = gameObject.GetComponent<GameManager>();

        isCalled = false;

        //Subscribe to Ad event once
        rewardBasedVideo = RewardBasedVideoAd.Instance;
        //Subscribe to Ad event once
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;

    }

    public void adButton()
    {
        isCalled = true;
    }

    void Update()
    {
        if(isCalled == true){
            AdRequest request = new AdRequest.Builder().Build();
            rewardBasedVideo.LoadAd(request, adUnitId);
            showAd();
        }
    }

    public void showAd()
    {
        if (rewardBasedVideo.IsLoaded())
        {
            rewardBasedVideo.Show();
        }
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        //Reawrd User here
        print("User rewarded with: " + amount.ToString() + " " + type);

        isCalled = false;

        managerScript.revival();
        managerScript.Loading.SetActive(false);
    }
}

回答1:

Likely because you registering to event multiple times when you call rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded; more than once. You should remove it from there and put it in the Start function instead.

string adUnitId = "ca-app-pub-5920324855307233/4458481507";
RewardBasedVideoAd rewardBasedVideo = null;

void Start()
{
    //Subscribe to Ad event once
    rewardBasedVideo = RewardBasedVideoAd.Instance;
    //Subscribe to Ad event once
    rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}

public void adButton()
{
   AdRequest request = new AdRequest.Builder().Build();
   rewardBasedVideo.LoadAd(request, adUnitId);
   showAd();
}

void Update()
{

}

public void showAd()
{
    print("IN showAd()");
    if (rewardBasedVideo.IsLoaded())
    {
        print("IN showAd() AND IsLoaded()");
        rewardBasedVideo.Show();
    }else{
        print("IN showAd() NOT IsLoaded()");
     }
}

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    print("IN HandleRewardBasedVideoRewarded()");
    IsLoaded()
    string type = args.Type;
    double amount = args.Amount;
    //Reawrd User here
    print("User rewarded with: " + amount.ToString() + " " + type);


    managerScript.revival();
    managerScript.Loading.SetActive(false);
}

public void OnDestroy()
{
    //Un-Subscribe to Ad event once
    rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
}