Search Unity

[SOLVED] Admob in Unity game- where to place the ad code?

Discussion in 'Scripting' started by polan31, Jun 15, 2018.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Like in title :

    Admob- where to place the ad code?

    (For Android)

    I put the ad code in place:

    -unused

    -AndroidAdId

    I did it well?

    Code (CSharp):
    1.  using UnityEngine;
    2.          using System.Collections;
    3.          using GoogleMobileAds.Api;
    4. public class AdScript : MonoBehaviour
    5. {
    6.      InterstitialAd interstitial;
    7.      bool hasShownAdOneTime;
    8.      // Use this for initialization
    9.      void Start()
    10.      {
    11.          RequestInterstitial();
    12.      }
    13.      void Update ()
    14.      {
    15.          if (TimeManager.isGameOver) {
    16.              if (!hasShownAdOneTime) {
    17.                  hasShownAdOneTime = true;
    18.                  showInterstitialAd ();
    19.              }
    20.          } else {
    21.              if (Gamer.isTimeToOver) {
    22.                  if (!hasShownAdOneTime) {
    23.                      hasShownAdOneTime = true;
    24.                      showInterstitialAd ();
    25.                  }
    26.              }
    27.          }
    28.      }
    29.      public void showInterstitialAd ()
    30.      {
    31.          if (interstitial.IsLoaded()) {
    32.              interstitial.Show ();
    33.          }
    34.      }
    35.      private void RequestInterstitial()
    36.      {
    37.          // These ad units are configured to always serve test ads.
    38.          #if UNITY_EDITOR
    39.          string adUnitId = "unused";
    40.          #elif UNITY_ANDROID
    41.          string adUnitId = "AndroidAdId";
    42.          #elif UNITY_IPHONE
    43.          string adUnitId = "unexpected_platform";
    44.          #else
    45.          string adUnitId = "unexpected_platform";
    46.          #endif
    47.          // Create an interstitial.
    48.          interstitial = new InterstitialAd (adUnitId);
    49.          interstitial.OnAdClosed += Interstitial_OnAdClosed;
    50.          AdRequest request = new AdRequest.Builder ().Build ();
    51.          interstitial.LoadAd (request);
    52.          }
    53.          private void Interstitial_OnAdClosed (object sender, System.EventArgs e)
    54.          {
    55.          RequestInterstitial ();
    56.          }
    57.          }
    I'm asking because I put it on yesterday, but it does not work, while the test add worked :)