- Home /
Unity Admob Implementing Interstitial
In my game I want to show interstitials when someone loses. I had banner ads working great, but then I changed my code to adjust for interstitials, and now it takes a while for my banner ad to show up, and when it does it lags for a second. I also am having trouble figuring out how to get my interstitials to pop up and then request another one. I have an ad manager and the way I am telling my game when to display an ad is when the death screen pops up, then an ad should be displayed, and when the death screen goes away, I want to request another ad. So far, I haven't been able to get it to work. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using GoogleMobileAds.Api;
public class AdManager : MonoBehaviour {
//public bool dead;
public int showAd;
public InterstitialAd interstitial;
public BannerView bannerView;
void Start () {
RequestBanner ();
showAd = Random.Range (1, 3);
RequestInterstitial ();
}
void Awake () {
DontDestroyOnLoad (this);
}
void Update () {
}
void ShowInterstitial () {
if (GameObject.Find ("Canvas1").GetComponent <Canvas>().enabled) {
if (showAd == 1) {
if (interstitial.IsLoaded ()) {
interstitial.Show ();
interstitial.Destroy ();
}
}
}
if (!GameObject.Find ("Canvas1").GetComponent <Canvas> ().enabled) {
RequestInterstitial ();
}
}
public void RequestInterstitial()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "xxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "xxxxxxxxxxxx";
#else
string adUnitId = "unexpected_platform";
#endif
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder()
.AddTestDevice ("xxxxxxxxxxxx")
.Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "xxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "xxxxxxxxxxxxx";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder()
//.AddTestDevice(AdRequest.TestDeviceSimulator) // Simulator.
.AddTestDevice("xxxxxxxxxxx") // My test device.
.Build();
// Load the banner with the request.
bannerView.LoadAd(request);
}
}
Answer by horatiu2004 · Sep 08, 2017 at 09:59 AM
Hello @UniluckStudios, what version of Unity are you using? As you can see the ADBannerView functionality is deprecated in Unity 2017, so be careful.
Regarding interstitial Ads, you should be "using UnityEngine.Advertisements;". Here is an example from Unity's User Manual:
using UnityEngine;
#if UNITY_ADS
using UnityEngine.Advertisements;
#endif
public class UnityAdsExample : MonoBehaviour
{
public void ShowDefaultAd()
{
#if UNITY_ADS
if (Advertisement.IsReady())
{
Advertisement.Show();
}
#endif
}
}
To give a reward, you can just pass your result callback from your advertisement like this:
const string RewardedPlacementId = "rewardedVideo";
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show(RewardedPlacementId, options);
From here just implement what you require in the HandleShowResult() method.
private void HandleShowResult(ShowResult result)
{
//Can use result to check if an add has Finished, Skipped or Failed
DoStuff();
}
Don't forget to Enable Ads from Services. To learn more see Unity's pages on the subject.