Example how to have google ad (interstitial) in a separate gameObject.
For this example we need to have two scenes:
- "menu"
- "stage"
**step one**
In the scene "menu" create empty GameObject and assign the following script to it
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using System;
public class ad : MonoBehaviour {
private InterstitialAd interstitial;
private string caAppPub = "ca-app-pub-9999999999999999/9999999999";
private bool loading = false;
public void RequestInterstitial(){
if (!loading) {
loading = true;
interstitial = new InterstitialAd (caAppPub);
AdRequest request = new AdRequest.Builder ().Build ();
interstitial.LoadAd (request);
interstitial.OnAdClosed += onAdClosed;
interstitial.OnAdLoaded += onAdLoaded;
}
}
private void onAdClosed(object obj, EventArgs args){
}
private void onAdLoaded(object obj, EventArgs args){
loading = false;
interstitial.Show ();
}
}
**step two**
Menu script with static "data" class.
Drag and drop "ad gameObject" to the adObject of the menu.
public class menu : MonoBehaviour {
public ad adObject;
void Start () {
data.ad = adObject;
DontDestroyOnLoad (data.ad);
}
}
public static class data {
public static ad ad;
}
**step three**
after this you would be able to call this static ad object from any scene,
like this:
data.ad.RequestInterstitial ();
↧