facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(複数sceneからAdMobを呼び出す)
(READ_PHONE_STATE権限が追加される問題)
行102: 行102:
  
 
==READ_PHONE_STATE権限が追加される問題==
 
==READ_PHONE_STATE権限が追加される問題==
これを追加してもよいが、要求が必要な広告が出ない可能性がある
+
READ_PHONE_STATEを追加するとよいとあったがREAD_PHONE_STATEの権限は消えなかった。
  
 
Assets/Plugins/Android/AndroidManifest.xml
 
Assets/Plugins/Android/AndroidManifest.xml
行108: 行108:
 
  <uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove" />
 
  <uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove" />
  
 
+
https://teratail.com/questions/95477
 
+
に書いてる通り、toolsを25.2.5に変更するとREAD_PHONE_STATE権限が消えた。
  
 
参考:https://teratail.com/questions/95477
 
参考:https://teratail.com/questions/95477
行116: 行116:
  
 
参考:https://blog.narumium.net/2018/02/26/%E3%80%90unity%E3%80%91%E5%8B%9D%E6%89%8B%E3%81%ABread_phone_state%E6%A8%A9%E9%99%90%E3%81%8C%E8%BF%BD%E5%8A%A0%E3%81%95%E3%82%8C%E3%82%8B/
 
参考:https://blog.narumium.net/2018/02/26/%E3%80%90unity%E3%80%91%E5%8B%9D%E6%89%8B%E3%81%ABread_phone_state%E6%A8%A9%E9%99%90%E3%81%8C%E8%BF%BD%E5%8A%A0%E3%81%95%E3%82%8C%E3%82%8B/
 +
 +
==複数sceneからAdMobを呼び出す==
 +
 +
AdMob.cs
 +
<pre>
 +
using GoogleMobileAds.Api;
 +
public class AdMob
 +
{
 +
    BannerView bannerView;
 +
    public void Init()
 +
    {
 +
        MobileAds.Initialize(GetAppId());
 +
    }
 +
    string GetAppId() {
 +
    {
 +
        #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
 +
        return appId;
 +
    }
 +
    string GetAdUnitId() {
 +
        #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
 +
        return adUnitId;
 +
    }
 +
    public void RequestBanner()
 +
    {
 +
        bannerView = new BannerView(GetAdUnitId(), AdSize.MediumRectangle, AdPosition.Center);
 +
        AdRequest request = new AdRequest.Builder().Build();
 +
        bannerView.LoadAd(request);
 +
    }
 +
    public void RequestBannerBottom()
 +
    {
 +
        bannerView = new BannerView(GetAdUnitId(), AdSize.MediumRectangle, AdPosition.Bottom);
 +
        AdRequest request = new AdRequest.Builder().Build();
 +
        bannerView.LoadAd(request);
 +
    }
 +
    public void Destroy()
 +
    {
 +
        bannerView.Destroy();
 +
        bannerView = null;
 +
    }
 +
}
 +
</pre>
 +
 +
呼び出す側
 +
<pre>
 +
AdMob admob;
 +
void Start()
 +
{
 +
    admob = new AdMob();
 +
    admob.Init();
 +
    admob.RequestBanner();
 +
}
 +
void OnClickPlay() {
 +
    admob.Destroy();
 +
    admob = null;
 +
    SceneManager.LoadScene ("PlayScene");
 +
}
 +
</pre>
  
 
==複数sceneからAdMobを呼び出す==
 
==複数sceneからAdMobを呼び出す==

2019年3月6日 (水) 00: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

iOSでError running CocoaPods. Please ensure you have at least version 1.0.0. You can install CocoaPods with the Ruby gem package manager:となる場合

sudo gem install -n /usr/local/bin cocoapods
pod setup

上をiosプロジェクト内でコマンド実行

iOSビルドで'GoogleMobileAds/GoogleMobileAds.h' file not foundエラーとなる場合

GoogleMobileAds.frameworkをPlugins/iOSに追加するか上のcocoapodをios_project内で実行する

Androidビルドでunable to merge android manifest see the console for more details エラー

2つAndroidManifest.xmlがありsdkバージョンなどが違うとこのエラーが出る。

Assets/Plugins/Android/AndroidManifest.xml
Assets/Plugins/Android/GoogleMobileAdsPlugin/AndroidManifest.xml

参考https://ict119.com/unity_admob_error/

AndroidビルドでError: Error while saving blame file, build will continueエラー

Assets/Plugins/Android/AndroidManifest.xml のxmlns:toolsがなければ追加

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.unity3d.player"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">

READ_PHONE_STATE権限が追加される問題

READ_PHONE_STATEを追加するとよいとあったがREAD_PHONE_STATEの権限は消えなかった。

Assets/Plugins/Android/AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove" />

https://teratail.com/questions/95477 に書いてる通り、toolsを25.2.5に変更するとREAD_PHONE_STATE権限が消えた。

参考:https://teratail.com/questions/95477

参考:https://qiita.com/matsuyoro/items/7a3f133a610c5c8c56d4

参考:https://blog.narumium.net/2018/02/26/%E3%80%90unity%E3%80%91%E5%8B%9D%E6%89%8B%E3%81%ABread_phone_state%E6%A8%A9%E9%99%90%E3%81%8C%E8%BF%BD%E5%8A%A0%E3%81%95%E3%82%8C%E3%82%8B/

複数sceneからAdMobを呼び出す

AdMob.cs

using GoogleMobileAds.Api;
public class AdMob
{
    BannerView bannerView;
    public void Init()
    {
        MobileAds.Initialize(GetAppId());
    }
    string GetAppId() {
    {
        #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
        return appId;
    }
    string GetAdUnitId() {
        #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
        return adUnitId;
    }
    public void RequestBanner()
    {
        bannerView = new BannerView(GetAdUnitId(), AdSize.MediumRectangle, AdPosition.Center);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
    }
    public void RequestBannerBottom()
    {
        bannerView = new BannerView(GetAdUnitId(), AdSize.MediumRectangle, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
    }
    public void Destroy()
    {
        bannerView.Destroy();
        bannerView = null;
    }
}

呼び出す側

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

複数sceneからAdMobを呼び出す

AdMob.cs

using GoogleMobileAds.Api;
public class AdMob
{
    BannerView bannerView;
    public void Init()
    {
        MobileAds.Initialize(GetAppId());
    }
    string GetAppId() {
    {
        #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
        return appId;
    }
    string GetAdUnitId() {
        #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
        return adUnitId;
    }
    public void RequestBanner()
    {
        bannerView = new BannerView(GetAdUnitId(), AdSize.MediumRectangle, AdPosition.Center);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
    }
    public void RequestBannerBottom()
    {
        bannerView = new BannerView(GetAdUnitId(), AdSize.MediumRectangle, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
    }
    public void Destroy()
    {
        bannerView.Destroy();
        bannerView = null;
    }
}

呼び出す側

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