It is necessary to introduce inter-page advertising AdMob in Unity 5. We look at the official Google manual , download, install the plugin. In the script we need we put
using GoogleMobileAds.Api; Well, we write the proposed code request interstitial
private void RequestInterstitial() { #if UNITY_ANDROID string adUnitId = "Пишем сюда наш пробный айди, тоже с руководства гугла"; #elif UNITY_IPHONE string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE"; #else string adUnitId = "unexpected_platform"; #endif // Initialize an InterstitialAd. InterstitialAd interstitial = new InterstitialAd(adUnitId); // Create an empty ad request. AdRequest request = new AdRequest.Builder().Build(); // Load the interstitial with the request. interstitial.LoadAd(request); } Up to this point, everything was fine, but then we are invited to show ads, creating the following method, for example, at the end of the game.
private void GameOver() { if (interstitial.IsLoaded()) { interstitial.Show(); } } And here the problems begin - first the red underlines the word interstitial , since we initialized it to private void RequestInterstitial() , and are trying to use it in private void GameOver() .
The obvious solution for me was to declare in the main method.
InterstitialAd interstitial; Underline ceased. But - does not work. Moreover, if you write something else to the SceneManager.LoadScene(0); method, for example, SceneManager.LoadScene(0); , it successfully throws you to the first level. But advertising does not appear, the method from which we referred to GameOver continues to spin, i.e. the game goes on.
Added for Alexey Shimansky :
@ Alexey, I am extremely grateful to you for your work, but I, in general, and in general, did it all over again. The problem is this - nothing goes! Therefore, he set off on all sorts of tricks with the combination of methods. He even specially created a new, clean project, imported this plug-in from Google official, did it your way - but there’s no sense. At all. The project is compiled but advertising is not displayed, although the conditions are simple - just click the UI button for GameOver. In general, the code and a few pictures. See the warnings in the pictures on the bottom line of Unity. Maybe it's in their case.
using UnityEngine; using GoogleMobileAds.Api; public class ads: MonoBehaviour { private InterstitialAd interstitial; //Жалуется на нее void Start() { RequestInterstitial(); } private void RequestInterstitial() {# if UNITY_ANDROID string adUnitId = "ca-app-pub-3940256099942544/1033173712";# elif UNITY_IPHONE string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";# else string adUnitId = "unexpected_platform";# endif // Initialize an InterstitialAd. InterstitialAd interstitial = new InterstitialAd(adUnitId); // Create an empty ad request. AdRequest request = new AdRequest.Builder().Build(); // Load the interstitial with the request. interstitial.LoadAd(request); } public void knopkaUI() { GameOver(); } // Update is called once per frame void Update() { } private void GameOver() { if (interstitial.IsLoaded()) { interstitial.Show(); } } } Pictures for clarity 1 2 3 4 Of course, the advertisement itself was tested on a phone with Wi-Fi on. I waited a minute while it was possible, the advertisement was loading, but to no avail - the button was pressed, but the advertisement did not appear. The AdMob user ID was taken from Google's examples when an application was being made in Android Studio on which a trial advertisement flew.

interstitial. - VladDinterstitialinRequestInterstitial. - VladD