Hello. I try to implement some ads in my projects but with no success. I tried with AdColony, ChartBoost and AdMob but non of the above was a success.
I tried AdMob again, following the integration guide, but still doesn't work.
Here is my script, I have in scene a button which triggers UserChoseToWatchAd() method.
What is wrong with it, what can I do to sole this problem, or is there a sample script for rewarded ad which I can copy paste?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using GoogleMobileAds.Api;
using UnityEngine.UI;
public class AdMobManager : MonoBehaviour
{
private RewardedAd rewardedAd;
private string adUnitId = "ca-app-pub-3940256099942544/5224354917";
public Text textField;
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { });
this.rewardedAd = new RewardedAd(adUnitId);
RequestRewardedAd();
}
private void RequestRewardedAd()
{
// Called when an ad request has successfully loaded.
this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
// Called when an ad request failed to load.
this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
// Called when an ad is shown.
this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
// Called when an ad request failed to show.
this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
// Called when the user should be rewarded for interacting with the ad.
this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
// Called when the ad is closed.
this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
this.rewardedAd.LoadAd(request);
}
#region RewardedAdsEvents
public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
textField.text = "HandleRewardedAdLoaded";
}
public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
{
textField.text = "HandleRewardedAdFailedToLoad event received with message: ";
}
public void HandleRewardedAdOpening(object sender, EventArgs args)
{
textField.text = "HandleRewardedAdOpening event received";
}
public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
{
textField.text = "HandleRewardedAdFailedToShow event received with message: ";
}
public void HandleRewardedAdClosed(object sender, EventArgs args)
{
textField.text = "HandleRewardedAdClosed event received";
this.RequestRewardedAd();
}
public void HandleUserEarnedReward(object sender, Reward args)
{
textField.text = "HandleRewardedAdRewarded event received for ";
}
#endregion
public void UserChoseToWatchAd()
{
if (this.rewardedAd.IsLoaded())
{
this.rewardedAd.Show();
}
}
}
↧