Проблема с Admob Interstitial Unity 3D - PullRequest
0 голосов
/ 11 января 2019

Я следовал пошаговому руководству Google Admob по внедрению промежуточной рекламы в моей игре 3D Unity, но эта вставка не отображалась при запуске игры на моем устройстве.

Знаете ли вы, что не так в моем коде, и как это исправить?

Сценарий объявления :

    private InterstitialAd interstitial;

    static int loadCount = 0;

    bool GameHasEnded = false;
    float RestartDelay = 1.5f;


    private void Start()
    {

        #if UNITY_ANDROID
           string appId = "ca-app-pub-3940256099942544/1033173712";
        #else
           string appId = "unexpected_platform";
        #endif

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        this.RequestInterstitial();

        if ((loadCount % 3) == 0)  // only show ad every third time
        {
            if (this.interstitial.IsLoaded())
            {
                this.interstitial.Show();
            }
        }
    }

    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().path);
        loadCount = loadCount + 1;
    }

    private void RequestInterstitial()
    {
       #if UNITY_ANDROID
           string adUnitId = "ca-app-pub-3940256099942544/1033173712";
       #else
           string adUnitId = "unexpected_platform";
       #endif

        // Initialize an InterstitialAd.
        this.interstitial = new InterstitialAd(adUnitId);

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();

        // Load the interstitial with the request.
        this.interstitial.LoadAd(request);
    }

Ответы [ 2 ]

0 голосов
/ 11 января 2019

Вы пытаетесь инициализировать admob и запрашивать новую вставку при каждом перезапуске. Вы должны создать один экземпляр AdmobController или любой другой и использовать его.

public class AdManager{

   public static AdManager instance;

   private void Awake(){
       if(instance == null) {
         instance = this;
         DontDestroyOnLoad(gameObject);
       } else {
         Destroy(gameObject);
         return;
       }
   }


   private void Start()
   {


       #if UNITY_ANDROID
          string appId = "ca-app-pub-3940256099942544/1033173712";
       #else
          string appId = "unexpected_platform";
       #endif

       // Initialize the Google Mobile Ads SDK.
       MobileAds.Initialize(appId);

       this.RequestInterstitial();

   }

    public void showInterstitial(){
      if ((loadCount % 3) == 0)  // only show ad every third time
      {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
     }

}

тогда, когда вы хотите показать промежуточный

AdController.instance.showInterstitial();

Возможно, у него синтаксическая ошибка или что-то в этом роде, но вы поняли.

0 голосов
/ 11 января 2019

Мне кажется, что проблема возникает, потому что загрузка промежуточного объявления занимает некоторое время (иногда до 3 секунд). Вы пытаетесь назвать это слишком быстро, и он не загружен, поэтому ничего не происходит. Я постоянно пытался показывать промежуточное объявление в void Update до тех пор, пока оно не показывало (используя bool с именем показано):

void Update () {
     if (shown == false) {
         ShowInterstitial();
     }
}
public void ShowInterstitial()
{
     if (interstitial.IsLoaded())
     {
         interstitial.Show();
         shown = true;
     } else    {
         Debug.Log ("Interstitial is not ready yet.");
     }    
}

Вы также можете загрузить объявление в начале уровня, а затем вызывать его только после окончания раунда.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...