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
}