Unityで作成したAndroidアプリにインタースティシャル広告を表示する方法
Unityで開発したAndroidアプリにインタースティシャル広告(全画面広告)を表示する方法です.
からDownload ZIPボタンを押しダウンロードする.
- 2. 解凍し,googleadmob.unitypackageをインポートする.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" android:installLocation="preferExternal" android:theme="@android:style/Theme.NoTitleBar" android:versionCode="1" android:versionName="1.0"> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true"/> <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:launchMode="singleTask" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- Admob Plugin value="true" --> <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" /> </activity> <!-- AdMob Plugin --> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> </application> <!-- AdMob Plugin --> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> </manifest>
- 4. GameObject > Create Empty にて空のGameObjectを追加する.
- 5. そのGameObject にgoogleadmob.unitypackageに含まれているAdMobPluginをアタッチする.
- 6. AdMobManager.cs として下記のスクリプトを作成し,上記のGameObjectにアタッチする.
using UnityEngine; using System.Collections; [RequireComponent(typeof(AdMobPlugin))] public class AdMobManager : MonoBehaviour { private const string AD_UNIT_ID = "YOUR_AD_UNIT_ID"; private const string INTERSTITIAL_ID = "YOUR_AD_UNIT_ID"; private AdMobPlugin admob; void Start() { admob = GetComponent<AdMobPlugin>(); admob.CreateBanner(adUnitId: AD_UNIT_ID, adSize: AdMobPlugin.AdSize.SMART_BANNER, isTopPosition: false, interstitialId: INTERSTITIAL_ID, isTestDevice: false); admob.RequestAd(); if (Random.value < 0.2f) { admob.RequestInterstitial(); } } void OnEnable() { AdMobPlugin.AdClosed += () => { Debug.Log ("AdClosed"); }; AdMobPlugin.AdFailedToLoad += () => { Debug.Log ("AdFailedToLoad"); }; AdMobPlugin.AdLeftApplication += () => { Debug.Log ("AdLeftApplication"); }; AdMobPlugin.AdOpened += () => { Debug.Log ("AdOpened"); }; AdMobPlugin.InterstitialClosed += () => { Debug.Log("InterstitialClosed"); }; AdMobPlugin.InterstitialFailedToLoad += () => { Debug.Log ("InterstitialFailedToLoad"); }; AdMobPlugin.InterstitialLeftApplication += () => { Debug.Log ("InterstitialLeftApplication"); }; AdMobPlugin.InterstitialOpened += () => { Debug.Log ("InterstitialOpened"); }; AdMobPlugin.AdLoaded += HandleAdLoaded; AdMobPlugin.InterstitialLoaded += HandleInterstitialLoaded; } void OnDisable() { AdMobPlugin.AdLoaded -= HandleAdLoaded; AdMobPlugin.InterstitialLoaded -= HandleInterstitialLoaded; } void HandleAdLoaded() { #if !UNITY_EDITOR admob.ShowBanner(); #endif } void HandleInterstitialLoaded() { #if !UNITY_EDITOR admob.ShowInterstitial(); #endif } }
AdMobManager.cs の下記箇所にAdMobで作成したIDを貼り付けます.
private const string AD_UNIT_ID = "YOUR_AD_UNIT_ID"; private const string INTERSTITIAL_ID = "YOUR_AD_UNIT_ID";
これで,Android用にBuildすると,画面表示時に画面下部にバナーと20%の確率で全画面広告が表示されるようになります.
AdMobManager.cs にある isTestDevice を True にすることで検証用の広告が表示されるようになります.
下記のアプリで適用していますので,よろしければ参考にしてくださいませ.
脳トレ - ポリオミノ - Google Play の Android アプリ
- 漢字 - なんどく!