Basically I'm trying to load a banner ad into my Unity 5 project and export to iOS.
Here is the code i'm calling inside of unity which is attached to a game object:
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using System;
public class AdController : MonoBehaviour {
InterstitialAd interstitial;
BannerView bannerView;
void Start () {
//------ Banner Ad -------
// Create a 320x50 banner at the top of the screen.
// Put your Admob banner ad id here
bannerView = new BannerView(
"ca-app-pub-xxxxxxxxxxxxxxxx", AdSize.SmartBanner, AdPosition.Top);
// Create ad request
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
bannerView.LoadAd(request);
bannerView.Show();
//---- Interstitial Ad -----
// Initialize an InterstitialAd.
// Put your admob interstitial ad id here:
interstitial = new InterstitialAd("ca-app-pub-xxxxxxxxxxxxxxx");
//Add callback for when ad is loaded
interstitial.AdLoaded += HandleAdLoaded;
// Create an ad request.
AdRequest requestInterstitial = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(requestInterstitial);
}
public void HandleAdLoaded(object sender, EventArgs args) {
interstitial.Show ();
}
void OnDestroy(){
if (interstitial!=null) {
interstitial.AdLoaded -= HandleAdLoaded;
interstitial.Destroy ();
}
if(bannerView!=null){
bannerView.Destroy ();
}
}
}
I'm using:
Has anyone got this to serve ads? Note: I did replace the xxxxx with the correct Ad Unit ID.
There is an project on GitHub:
Unity Admob Plugin
it easy to use,and I have success with this.
using admob;
...
Admob.Instance().initAdmob("admob banner id", "admob interstitial id");//admob id with format ca-app-pub-2796046890663330/756767388
Admob.Instance().showBannerRelative(AdSize.Banner, AdPosition.BOTTOM_CENTER, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With