facebook twitter hatena line email

「Unity/GoogleMobileAds/Banner」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(位置)
行75: 行75:
 
  AdPosition.Center
 
  AdPosition.Center
  
==
+
==複数sceneからAdMobを呼び出す==
  
 
AdMob.cs
 
AdMob.cs
行86: 行86:
 
     {
 
     {
 
         #if UNITY_ANDROID
 
         #if UNITY_ANDROID
             string appId = "ca-a5730173711";
+
             string appId = "ca-app-pub-3940256099942544~3347511713";
 
         #elif UNITY_IPHONE
 
         #elif UNITY_IPHONE
             string appId = "ca-app-pub-32511";
+
             string appId = "ca-app-pub-3940256099942544~1458002511";
 
         #else
 
         #else
 
             string appId = "unexpected_platform";
 
             string appId = "unexpected_platform";
行94: 行94:
 
         MobileAds.Initialize(appId);
 
         MobileAds.Initialize(appId);
 
     }
 
     }
 
 
     public void RequestBanner()
 
     public void RequestBanner()
 
     {
 
     {
 
         #if UNITY_ANDROID
 
         #if UNITY_ANDROID
             string adUnitId = "ca-app-pub1763008541";
+
             string adUnitId = "ca-app-pub-3940256099942544/6300978111";
 
         #elif UNITY_IPHONE
 
         #elif UNITY_IPHONE
             //string adUnitId = "ca-35716";
+
             string adUnitId = "ca-app-pub-3940256099942544/2934735716";
 
         #else
 
         #else
 
             string adUnitId = "unexpected_platform";
 
             string adUnitId = "unexpected_platform";
行113: 行112:
 
         bannerView = null;
 
         bannerView = null;
 
     }
 
     }
 +
}
 +
</pre>
 +
 +
呼び出し元.cs
 +
<pre>
 +
AdMob admob;
 +
void Start()
 +
{
 +
    admob = new AdMob();
 +
    admob.Init();
 +
    admob.RequestBanner();
 +
}
 +
void OnClickPlay() {
 +
    admob.Destroy();
 +
    admob = null;
 +
    SceneManager.LoadScene ("PlayScene");
 
}
 
}
 
</pre>
 
</pre>

2019年3月3日 (日) 09:59時点における版

admob申し込み

https://support.google.com/admob/answer/7356219?hl=ja

アプリID ca-app-pub-0000000000000000~0000000000

広告ID ca-app-pub-0000000000000000/0000000000

などを手に入れる

UnityのAdMobのpluginsのインストール

https://github.com/googleads/googleads-mobile-unity/releases

Assets/ImportPackageからunitypluginを追加

サンプル

using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private BannerView bannerView;
    public void Start()
    {
        #if UNITY_ANDROID
            string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
            string appId = "unexpected_platform";
        #endif

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

    private void RequestBanner()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/6300978111";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/2934735716";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
    }
}

参考: https://developers.google.com/admob/unity/banner

広告削除

bannerView.Hide();
bannerView.Destroy();

サイズ

AdSize.Banner 320×50	標準のバナー
AdSize.LargeBanner 320×100	バナー(大)
AdSize.MediumRectangle 300×250	IAB レクタングル(中)
AdSize.FullBanner 468×60	IAB フルサイズ バナー
AdSize.Leaderboard 728×90	IAB ビッグバナー
AdSize.SmartBanner  画面の幅×32|50|90

位置

AdPosition.Top
AdPosition.Bottom
AdPosition.TopLeft
AdPosition.TopRight
AdPosition.BottomLeft
AdPosition.BottomRight
AdPosition.Center

複数sceneからAdMobを呼び出す

AdMob.cs

using GoogleMobileAds.Api;
public class AdMob
{
    BannerView bannerView;
    public void Init()
    {
        #if UNITY_ANDROID
            string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
            string appId = "unexpected_platform";
        #endif
        MobileAds.Initialize(appId);
    }
    public void RequestBanner()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/6300978111";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/2934735716";
        #else
            string adUnitId = "unexpected_platform";
        #endif
        bannerView = new BannerView(adUnitId, AdSize.MediumRectangle, AdPosition.Center);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
    }
    public void Destroy()
    {
        bannerView.Destroy();
        bannerView = null;
    }
}

呼び出し元.cs

AdMob admob;
void Start()
{
    admob = new AdMob();
    admob.Init();
    admob.RequestBanner();
}
void OnClickPlay() {
    admob.Destroy();
    admob = null;
    SceneManager.LoadScene ("PlayScene");
}