The ads are not showing, I think because there are off the screen.. But I can't figure oute the problem.
Hope there is the right section and sorry for the bad English.
Under the MenuAds
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class MenuAds : MonoBehaviour {
private BannerView bannerView;
private InterstitialAd interstitial;
private NativeExpressAdView nativeExpressAdView;
public void Start()
{
#if UNITY_ANDROID
string appId = "ca-app-pub-9369507388231074/5771936825";
#elif UNITY_IPHONE
string appId = "ca-app-pub-9369507388231074/5771936825";
#else
string appId = "unexpected_platform";
#endif
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(appId);
}
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddKeyword("game")
.SetGender(Gender.Male)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
private void DestroyBanner()
{
// Clean up banner ad before creating a new one.
if (this.bannerView != null)
{
this.bannerView.Destroy();
}
}
private void DestroyNative()
{
// Clean up interstitial ad before creating a new one.
if (this.interstitial != null)
{
this.interstitial.Destroy();
}
}
public void RequestBanner()
{
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#else
string adUnitId = "unexpected_platform";
#endif
// Clean up banner ad before creating a new one.
if (this.bannerView != null)
{
this.bannerView.Destroy();
}
// Create a 320x50 banner at the top of the screen.
this.bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
// Register for ad events.
this.bannerView.OnAdLoaded += this.HandleAdLoaded;
this.bannerView.OnAdFailedToLoad += this.HandleAdFailedToLoad;
this.bannerView.OnAdOpening += this.HandleAdOpened;
this.bannerView.OnAdClosed += this.HandleAdClosed;
this.bannerView.OnAdLeavingApplication += this.HandleAdLeftApplication;
// Load a banner ad.
this.bannerView.LoadAd(this.CreateAdRequest());
this.bannerView.Show();
}
public void RequestInterstitial()
{
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#else
string adUnitId = "unexpected_platform";
#endif
// Clean up interstitial ad before creating a new one.
if (this.interstitial != null)
{
this.interstitial.Destroy();
}
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
this.interstitial.OnAdLoaded += this.HandleInterstitialLoaded;
this.interstitial.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
this.interstitial.OnAdOpening += this.HandleInterstitialOpened;
this.interstitial.OnAdClosed += this.HandleInterstitialClosed;
this.interstitial.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
public void RequestNativeExpressAdView()
{
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-9369507388231074/5771936825";
#else
string adUnitId = "unexpected_platform";
#endif
// Clean up native express ad before creating a new one.
if (this.nativeExpressAdView != null)
{
this.nativeExpressAdView.Destroy();
}
// Create a 320x150 native express ad at the top of the screen.
this.nativeExpressAdView = new NativeExpressAdView(
adUnitId,
new AdSize(320, 150),
AdPosition.Top);
// Register for ad events.
this.nativeExpressAdView.OnAdLoaded += this.HandleNativeExpressAdLoaded;
this.nativeExpressAdView.OnAdFailedToLoad += this.HandleNativeExpresseAdFailedToLoad;
this.nativeExpressAdView.OnAdOpening += this.HandleNativeExpressAdOpened;
this.nativeExpressAdView.OnAdClosed += this.HandleNativeExpressAdClosed;
this.nativeExpressAdView.OnAdLeavingApplication += this.HandleNativeExpressAdLeftApplication;
// Load a native express ad.
this.nativeExpressAdView.LoadAd(this.CreateAdRequest());
this.nativeExpressAdView.Show();
}
public void ShowInterstitial()
{
if (this.interstitial.IsLoaded())
{
this.interstitial.Show();
}
else
{
MonoBehaviour.print("Interstitial is not ready yet");
}
}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialLoaded event received");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print(
"HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialOpened event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialLeftApplication event received");
}
#endregion
#region Native express ad callback handlers
public void HandleNativeExpressAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleNativeExpressAdAdLoaded event received");
}
public void HandleNativeExpresseAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print(
"HandleNativeExpressAdFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleNativeExpressAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleNativeExpressAdAdOpened event received");
}
public void HandleNativeExpressAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleNativeExpressAdAdClosed event received");
}
public void HandleNativeExpressAdLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleNativeExpressAdAdLeftApplication event received");
}
#endregion
}
Under the GameManager
void Vittoria()
{
if (score1 == 5)
{
panel.SetActive(true);
Exit.SetActive(true);
ExitB.SetActive(true);
player1Won.SetActive(true);
vittoria = true;
GameObject go = GameObject.Find("MainCamera");
//MenuAds ads = (MenuAds)go.GetComponent(typeof(MenuAds));
MenuAds ads = go.GetComponent();
ads.RequestNativeExpressAdView();
}
if (score2 == 5)
{
panel.SetActive(true);
Exit.SetActive(true);
ExitB.SetActive(true);
player2Won.SetActive(true);
vittoria = true;
GameObject go = GameObject.Find("MainCamera");
//MenuAds ads = (MenuAds)go.GetComponent(typeof(MenuAds));
MenuAds ads = go.GetComponent();
ads.RequestNativeExpressAdView();
}
}
Under the MenuAdsScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuAdsScript : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject go = GameObject.Find("MainCamera");
MenuAds ads = (MenuAds)go.GetComponent(typeof(MenuAds));
ads.RequestBanner();
}
// Update is called once per frame
void Update () {
}
}
The console message
![alt text][1]
[1]: /storage/temp/105831-catturdsfa.png
↧