- Home /
Displaying Interstitial Ads Correctly ? Please Help.
Hi there I am having trouble in using the official google ads plugin to display some ads on exit of a game mode. Correctly as you see below this script is called when player dies or pauses the game. When this script is activated it requests a ad on start and when exit is clicked it displays a interstitial ad. The trouble with this is the exit button can be clicked so fast before even ad is loaded or requested so no ad is even displayed! How can I load the request on start of a scene so when exit is pressed it already ready to be shown always? Thank you. As the requests seems like takes few seconds..by that time the player is long gone avoided the ad haha
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
/// <summary>
/// Start or quit the game
/// </summary>
public class GameOverScriptP : MonoBehaviour
{
private GUISkin skin;
private BannerView bannerView;
private InterstitialAd interstitial;
bool paused = false;
void Start ()
{
// Load a skin for the buttons
skin = Resources.Load ("GUISkin") as GUISkin;
RequestInterstitial();
}
void OnGUI ()
{
const int buttonWidth = 170;
const int buttonHeight = 80;
// Set the skin to use
GUI.skin = skin;
if (
GUI.Button (
// Center in X, 1/3 of the height in Y
new Rect (
Screen.width / 2 - (buttonWidth / 2),
(1 * Screen.height / 3) - (buttonHeight / 2),
buttonWidth,
buttonHeight
),
"Retry"
)
) {
// Reload the level
Application.LoadLevel ("Pacifism");
KeepingScorePacifism.Score = 0;
Time.timeScale = 1;
}
if (
GUI.Button (
// Center in X, 2/3 of the height in Y
new Rect (
Screen.width / 2 - (buttonWidth / 2),
(2 * Screen.height / 3) - (buttonHeight / 2),
buttonWidth,
buttonHeight
),
"Exit"
)
) {
// Reload the level
Application.LoadLevel ("TitleScreen");
KeepingScorePacifism.Score = 0;
Time.timeScale = 1;
ShowInterstitial ();
}
}
private void RequestInterstitial ()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd (adUnitId);
// Register for ad events.
interstitial.AdLoaded += HandleInterstitialLoaded;
interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.AdOpened += HandleInterstitialOpened;
interstitial.AdClosing += HandleInterstitialClosing;
interstitial.AdClosed += HandleInterstitialClosed;
interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
// Load an interstitial ad.
interstitial.LoadAd (createAdRequest ());
}
// Returns an ad request with custom ad targeting.
private AdRequest createAdRequest ()
{
return new AdRequest.Builder ()
.AddTestDevice (AdRequest.TestDeviceSimulator)
.AddTestDevice ("0123456789ABCDEF0123456789ABCDEF")
.AddKeyword ("game")
.SetGender (Gender.Male)
.SetBirthday (new DateTime (1985, 1, 1))
.TagForChildDirectedTreatment (false)
.AddExtra ("color_bg", "9B30FF")
.Build ();
}
private void ShowInterstitial ()
{
if (interstitial.IsLoaded ()) {
interstitial.Show ();
} else {
print ("Interstitial is not ready yet.");
}
}
#region Banner callback handlers
public void HandleAdLoaded (object sender, EventArgs args)
{
print ("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad (object sender, AdFailedToLoadEventArgs args)
{
print ("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened (object sender, EventArgs args)
{
print ("HandleAdOpened event received");
}
void HandleAdClosing (object sender, EventArgs args)
{
print ("HandleAdClosing event received");
}
public void HandleAdClosed (object sender, EventArgs args)
{
print ("HandleAdClosed event received");
}
public void HandleAdLeftApplication (object sender, EventArgs args)
{
print ("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded (object sender, EventArgs args)
{
print ("HandleInterstitialLoaded event received.");
}
public void HandleInterstitialFailedToLoad (object sender, AdFailedToLoadEventArgs args)
{
print ("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened (object sender, EventArgs args)
{
print ("HandleInterstitialOpened event received");
}
void HandleInterstitialClosing (object sender, EventArgs args)
{
print ("HandleInterstitialClosing event received");
}
public void HandleInterstitialClosed (object sender, EventArgs args)
{
print ("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication (object sender, EventArgs args)
{
print ("HandleInterstitialLeftApplication event received");
}
#endregion
}
ref to the doc https://github.com/unity-plugins/Unity-Admob
i'm using this plugin but i cant control my interstitial ad, it shows all the time , how to make it close on ad close, please help :(
Answer by smallbit · Mar 02, 2015 at 05:04 AM
First of all, when you reload the level the code after that is not executed. (see your code)
What I do handling ads, is to create a adController script which has dontdestroyonload in it. Whenever i precache the ad - it stays in the memory even if i change level. I start caching the new add as soon as the app starts, and than assuming that player cannot get to actually play the game and finish it or die in less than few secs you should be fine.
Next thing I do is when I got to result screen and actually have add to show (interstitial.IsLoaded ()) I block all the buttons and display the add using a global boolean lets say canUserClickButtons.
Than when the interstitial.AdClosed event fires I set the flag back to true so user can proceed. This event will be fired when you click back button on android or close the add clicking on cross button. Have in mind that admob interstitial is not closing upon clicking, so when you go back to your game after clicking it's still visible, hence for simple implementation it's the only event to handle.
Another thing is that once the ad is showed you should pre-cache the new one (also destoying the previous one upon closing, it has a destroy method available). And finally to increase the chance, that any add will be shown I normally implement two different networks i.e chartboost and admob.
hope it helps
Thank you I have just did that. I used don't destroy on load to load the ad before the game mode begins. So when player exits it is ready to be shown without fail or delay. I just placed it on exit so not to spam players during gameplay or when they die.
One last question how do I properly use
interstitial.Destroy();
so I can let go of interstitial properly as per read me on googleadmob github page? and also mentioned by yourself. Right on when the script starts at menu it
void Start()
{
RequestInterstitial();
}
and when I destroy it ( it destroys when player exits but comes back on the main menu )
void OnDestroy()
{
ShowInterstitial();
}
Thank you.
In your case, if you always request new add after showing previous one, I think destroy would be redundant and thus is not needed.
This is pretty much what I do. Just make sure you also add an event handler for when the ad fails to load and enable the UI - otherwise the user might be stuck on a non-functional UI.
Your answer
Follow this Question
Related Questions
Admob iOS Rewarded Videos don't close 0 Answers
Banner Ads which contain animated ads affect the performance of the game !! 2 Answers
Admob interstitial ads works, but rewarded video doesn't work 0 Answers
The ads on my game runs when I run it on development build but not in release build. 1 Answer
GMA Unity Plugin v3.16 not showing ads. 0 Answers