- Home /
Question by
lennotoecom · May 24, 2017 at 07:40 AM ·
unity 5adsgoogle
unity google interstitial ad
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 ();
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612122526im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Stopped Google Ads Marketing for Apple Store game 0 Answers
unity ads after google play publishing ?? 0 Answers
Copyright ? 2 Answers