Search Unity

Interstitial show back

Discussion in 'Android' started by ufukoztrkk, Jan 31, 2019.

  1. ufukoztrkk

    ufukoztrkk

    Joined:
    Dec 19, 2018
    Posts:
    2
    Only when starting the game is calling interstitial.
    How do I get the Interstitial show in all three games? what should I do?

    periodically I want to call but could not find. my basic code is below


    Code (CSharp):
    1.  
    2.   using System.Collections;
    3.   using System.Collections.Generic;
    4.   using UnityEngine;
    5.   using GoogleMobileAds.Api;
    6.  
    7.   public class Interstitial : MonoBehaviour
    8.  
    9.   {
    10.       InterstitialAd interstitial;
    11.  
    12.       // Use this for initialization
    13.  
    14.       void Start()
    15.       {
    16.   #if UNITY_ANDROID
    17.           string appId = "Admob ID";
    18.   #elif UNITY_IPHONE
    19.               string appId = "ca-app-pub-3940256099942544~1458002511";
    20.   #else
    21.           string appId = "unexpected_platform";
    22.   #endif
    23.  
    24.           // Initialize the Google Mobile Ads SDK.
    25.           MobileAds.Initialize(appId);
    26.  
    27.  
    28.      
    29.      
    30.          
    31.      
    32.   #if UNITY_ANDROID
    33.       string adUnitId = "ca-app-pub-3940256099942544/1033173712";
    34.   #elif UNITY_IPHONE
    35.           string adUnitId = "ca-app-pub-3940256099942544/4411468910";
    36.   #else
    37.           string adUnitId = "unexpected_platform";
    38.   #endif
    39.  
    40.           // Initialize an InterstitialAd.
    41.           this.interstitial = new InterstitialAd(adUnitId);
    42.  
    43.  
    44.  
    45.           // Create an empty ad request.
    46.           AdRequest request = new AdRequest.Builder()
    47.               .AddTestDevice("2077ef9a63d2b398840261c8221a0c9b")
    48.               .Build();
    49.           // Load the interstitial with the request.
    50.           this.interstitial.LoadAd(request);
    51.  
    52.  
    53.  
    54.       }
    55.  
    56.       void Update()
    57.  
    58.       {
    59.  
    60.           if (this.interstitial.IsLoaded())
    61.  
    62.           {
    63.               this.interstitial.Show();
    64.  
    65.  
    66.           }
    67.  
    68.                  
    69.  
    70.  
    71.       }
    72.  
    73.       void OnDestroy()
    74.       {
    75.  
    76.           interstitial.Destroy(); //Destroy
    77.  
    78.          
    79.       }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    With this code, you are trying to show an ad about 60 times a second in your Update method. Only call Show when you need it!
     
  3. ufukoztrkk

    ufukoztrkk

    Joined:
    Dec 19, 2018
    Posts:
    2
    So what to do.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry, I don't follow. You have Show working, only call it when you need to! Where in your game do you want ads to show? That is where you place your Show method. You mention "all three games", did you mean scenes? For starters as you are learning the platform, I might suggest putting a test button on your scene, and showing an ad in the click event for the button, and go from there.