甘酒のアプリ開発

一人でも多くの人を幸せにするアプリを作っていけたらなぁと思います。

【Unity】3回に1回インタースティシャル広告を表示する方法。

f:id:RenRoku6:20211030200200j:plain

 

今回は、3回に1回インタースティシャル広告を表示する方法 について紹介していきます。

 

簡単に実装することができるので、もしよかったら試してみてください。

 

 

 

スクリプトの例

using GoogleMobileAds.Api;
using System;
using UnityEngine;

public class Interstitial_3KAI : MonoBehaviour//パネルが3回表示されたらインタースティシャル広告を表示するスクリプト
{
   private InterstitialAd interstitial;
   public int InterstitialCount;

   void Start()
    {
        RequestInterstitial();
    }

   void OnEnable()
    {
        InterstitialCount++;
        if (InterstitialCount >= 3) { Invoke("ShowInterstitialAd", 0); }
    }

   public void RequestInterstitial()
    {
       string adUnitId = "ca-app-pub-3940256099942544/1033173712";

       interstitial = new InterstitialAd(adUnitId);
        DestroyInterstitialAd();

       interstitial.OnAdClosed += HandleOnAdClosed;

       AdRequest request = new AdRequest.Builder().Build();

       interstitial.LoadAd(request);
    }

   public void ShowInterstitialAd()//広告を表示する処理
    {
       if (interstitial.IsLoaded())
        {
            interstitial.Show();
        }
    }

   public void HandleOnAdClosed(object sender, EventArgs args)//広告を「閉じる」または「戻る」したときの処理
   {
        InterstitialCount = 0;
        RequestInterstitial();
    }

    public void DestroyInterstitialAd()// 一度リセット:次の広告表示の前に実行しておく必要あり
   {
        interstitial.Destroy();
    }
}

上記のような スクリプトを作ります。

そのままコピペしてもらえればOKです。

 

 

スクリプトの説明

 

 

private InterstitialAd interstitial;
public int InterstitialCount;

インタースティシャル広告を格納するための変数パネルを表示した回数をカウントするための変数を用意します。

 

void Start()
{
  RequestInterstitial();
}

ゲーム開始時にRequestInterstitial()が実行されます。

 

void OnEnable()
{
  InterstitialCount++;
  if (InterstitialCount >= 3) { Invoke("ShowInterstitialAd", 0); }
}

OnEnableメソッドを使います。

(※OnEnableメソッド  ゲームオブジェクトがアクティブになった時に呼ばれるメソッドです。)

 

パネルが表示されたときにInterstitialCountが +1されます。

もしInterstitialCountが3以上だった場合ShowInterstitialAd()が実行されます。

 

 

 

public void RequestInterstitial()
{
   string adUnitId = "ca-app-pub-3940256099942544/1033173712";

   interstitial = new InterstitialAd(adUnitId);
  DestroyInterstitialAd();

   interstitial.OnAdClosed += HandleOnAdClosed;

   AdRequest request = new AdRequest.Builder().Build();

   interstitial.LoadAd(request);
}

広告をリクエストする処理です。

 

public void ShowInterstitialAd()//広告を表示する処理
{
   if (interstitial.IsLoaded())
  {
            interstitial.Show();
  }
}

広告を表示する処理です。

 

public void HandleOnAdClosed(object sender, EventArgs args)//広告を「閉じる」または「戻る」したときの処理
{
  InterstitialCount = 0;
  RequestInterstitial();
}

広告を「閉じる」または「戻る」したときの処理です。

InterstitialCountを0にします。

RequestInterstitial()が実行されます。

 

 


public void DestroyInterstitialAd()// 一度リセット:次の広告表示の前に実行しておく必要あり
{
  interstitial.Destroy();
}

 

まとめ

今回は3回に1回インタースティシャル広告を表示する方法を紹介しました。

 

ゲームオーバー時など、特定のタイミングで表示されるオブジェクトに今回紹介したスクリプトを張り付ければ、3回に1回インタースティシャル広告を表示させることができます。

 

なかなか実装が面倒くさいAdMob広告ですが、一度理解してしまえば簡単に実装することができるので、ぜひマスターしてみてください。

 

もし誰かのお役に立つことができれば幸いです。

 

 

終わり。。