I have tried to adjust, format the code in every shape and form, this is the code that I most recently used and still does not work!
private InterstitialAd interstitial;
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape)) {
ShowInterstitial();
}
}
private void RequestInterstitial()
{
interstitial = new InterstitialAd("ca-app-pub-4978585600967291/*******");
// Create an empty ad request.
AdRequest request = new AdRequest.Builder ().Build ();
// Load the interstitial with the request.
interstitial.LoadAd (request);
}
private void ShowInterstitial()
{
interstitial.Show();
}
else
{
print("Interstitial is not ready yet.");
}
}
}
What I am trying to do is, when the user presses the back button the on Android, it will show the ad, then if they press it again, the application quits.
Here is the code that made the Interstitial Ads work.
Does anyone know how to make when the user press the back button once, it shows the ad, next press it exits the program?
Here is the code (only shows the Interstitial Ad when the back button is pressed) If anyone uses it, don’t forget to install the AdMob Plugin and create the Jar file (How to Integrate Admob with Unity for Android - Tutorial - YouTube) replace YOURFILENAME, with the name you named the .cs file. The private bool is important to make the interstitial work! thus why its there lol.
using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class YOURFILENAME : MonoBehaviour {
public InterstitialAd interstitial;
private bool adIsOn;
void Update ()
{
if (adIsOn != true) {
if (interstitial.IsLoaded ()) {
if (Input.GetKeyDown (KeyCode.Escape)) {
adIsOn = true;
interstitial.Show ();
}
}
}
}
private void Start()
{
interstitial = new InterstitialAd("PUT ADMOB ID NUMBER HERE");
// Create an empty ad request.
AdRequest request = new AdRequest.Builder ().Build ();
// Load the interstitial with the request.
interstitial.LoadAd (request);
}
}