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.

  • 2
    Uh ... Do you know how a class field differs from a local variable? - VladD
  • one
    Okay. Now think about what your interstitial . - VladD
  • one
    If you run the program to compile, usually the compiler tells you an error in text form. This time. Also, consider what interstitial in RequestInterstitial . - VladD
  • one
    For example, in the fact that you create it and write to a local variable. And how will he be in the field with the same name? - VladD
  • one
    If you could achieve compilability, better. Now just trace in the debugger. - VladD

1 answer 1

In the comments @VlaD hinted to you, what is the most likely problem:

You declare an interstitial variable with type InterstitialAd in the RequestInterstitial method and initialize it there. A variable declared inside a method lives only inside it. As soon as the method works, the variable dies, which means it cannot be addressed in other methods. These are the basics not so much of a unit as of programming in general.

Why did Google write it this way and not otherwise? Most likely because he is counting on your competence and that you know these very basics and therefore he does not need to chew why in one example the variable is declared and initialized, and in another it is supposedly used.

output at least two:

First: declare a variable in the class itself and only then use it in various methods:

 class Test : MonoBehaviour { private InterstitialAd interstitial; void Start(){ // инициализируем interstitial; RequestInterstitial(); } // здесь метод инициализации private void RequestInterstitial() { ... ... // InterstitialAd interstitial = new InterstitialAd(adUnitId); <--- уже неправильно! interstitial = new InterstitialAd(adUnitId); ... } private void GameOver() { // здесь interstitial доступна, т.к. видна внутри класса всем методам if (interstitial.IsLoaded()) { interstitial.Show(); } } } 

Second:

There is a ready-made plug-in for Unity on one resource called GitHub . Called admob-unity-plugin and located here along with the docks .

Everything is ready there, practically. It remains only to twist the settings. Download the package. It has an AdMobPlugin set- AdMobPlugin , we throw it on the stage. We see:

enter image description here

In publisher id drive in your ID. Everything.

HappyEnd!


For the test, turn on the isTesting . On the production - disable. With the settings you can play.

AdMobPluginDebug - you just need to play AdMobPluginDebug with the settings (where the banner will be located, etc.), by poking the buttons on the screen. They are just shown in the screenshot above. On the production of this component of course it will be necessary to turn off, so that on the screen of these buttons was not))

AdMobPluginMockup - it was possible directly in the editor to see how the banner looks and to see it directly (in the screenshot it is below in the center). Also this component, so that it is convenient to place everything where you need it and turn it off at the production ...

As a result: AdMobPluginDebug and AdMobPluginMockup - only for the editor, and for the convenience of the visual - how everything will look. Disable on sale.

Video installation

  • I am extremely grateful to you for your work, but I, in general, and generally did so at first. 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. - Dmitrii
  • one
    @Dmitrii what are the warnings? - Alexey Shimansky
  • Do not allow to publish everything in the comments, too much. Now I’ll edit the main post, look in a minute - Dmitrii
  • one
    @Dmitrii and the plugin is not set? the same is easier - Alexey Shimansky
  • I need interstitial advertising. It is not in that plugin, it was updated 2 years ago. - Dmitrii