MobileAds クラスは、Google Mobile Ads Unity Plugin のグローバル設定を提供します。
Unity のメインスレッドで広告イベントを発生させる
Google Mobile Ads Unity Plugin は、Unity のメインスレッドとは異なるスレッドでイベントを発生させます。広告イベントを実装して Unity オブジェクトを操作する場合は、Google Mobile Ads Unity Plugin イベントを Unity メインスレッドと同期する必要があります。
広告イベントを Unity のメインスレッドと同期します。手動で同期することも、Google Mobile Ads Unity Plugin に同期を処理させて自動的に同期することもできます。
推奨: 広告イベントを手動で同期する
広告イベントを手動で同期するには、メインスレッドで ExecuteInUpdate メソッドを使用します。UnityEngine オブジェクトを操作する場合、RaiseAdEventsOnUnityMainThread プロパティが無効になっている間は、ExecuteInUpdate メソッドを使用する必要があります。
次の例では、バックグラウンド スレッドをログに記録し、UnityEngine オブジェクトを操作するアクションを実行します。
// Google Mobile Ads events are raised off the Unity main thread.
// This log is executed off the Unity main thread.
// Write all time-sensitive code before ExecuteInUpdate().
Debug.Log("Executing off the Unity main thread.");
// Use ExecuteInUpdate to run code on the main thread, allowing you to
// interact with Unity UI and GameObjects.
// Changed to fully-qualified name to resolve CS0103
GoogleMobileAds.Common.MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
// This callback may be delayed on Android until the user returns to the app.
Debug.Log("Executing on the Unity main thread.");
// Place all code that interacts with Unity UI and GameObjects inside this callback.
if (_myGameObject != null)
{
_myGameObject.SetActive(true);
}
});
広告イベントの同期を自動化する
Google Mobile Ads Unity Plugin で広告イベントを同期するには、MobileAds.RaiseAdEventsOnUnityMainThread プロパティを true に設定します。
...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// When true all events raised by GoogleMobileAds will be raised
// on the Unity main thread. The default value is false.
MobileAds.RaiseAdEventsOnUnityMainThread = true;
}
}
動画広告のボリューム調整
アプリに独自の音量調整機能(音楽や効果音のカスタム音量設定など)が備わっている場合、アプリの音量設定を Google Mobile Ads Unity Plugin に開示すれば、動画広告の音量にもアプリ側の音量設定を反映できます。これにより、動画広告をユーザーの想定どおりの音量で視聴してもらうことが可能になります。
デバイスの音声出力の大きさは、デバイスのボリューム(音量ボタンや OS レベルのボリューム スライダーで制御)で決まります。アプリでは音声の聞こえ方を独自にコントロールするために、デバイスの設定に対する相対的なボリューム レベルを独自に調節できます。
アプリ側で設定されている相対音量を Google Mobile Ads Unity Plugin に伝えるには、広告を読み込む前に SetApplicationVolume() メソッドを呼び出します。広告の音量設定値の有効範囲は、0.0(無音)から 1.0(デバイスの現在の音量)までです。アプリ側の相対音量設定を SDK に伝えるコードは、たとえば次のようになります。
// Set app volume to be half of current device volume.
MobileAds.SetApplicationVolume(0.5f);
アプリ側で音声がミュートされたことを SDK に伝えるには、広告を読み込む前に SetApplicationMuted() メソッドを呼び出します。
// Set app to be muted.
MobileAds.SetApplicationMuted(true);
アプリ側の音量は、デフォルトでは 1(デバイスの現在の音量)に設定されており、アプリはミュートされていない状態です。
Cookie の使用に対する同意
アプリに特別な要件がある場合は、オプションの ApplicationPreferences キー gad_has_consent_for_cookies を 0 に設定して、制限付き広告を有効にできます。
// Enable limited ads
ApplicationPreferences.SetInt("gad_has_consent_for_cookies", 0);
Android の圧縮
この Unity の公開オプションを使用すると、Java コードの圧縮を有効にできます。圧縮を有効にする場合は、SDK が参照するクラスを保持するために、カスタムの ProGuard ファイルも作成する必要があります。
カスタム Proguard ファイルを有効にする
[Project Settings] > [Player] > [Android] > [Publishing Settings] > [Build] に移動し、以下を選択します。
- カスタム Proguard ファイル
/Assets/Plugins/Android/proguard-user.txtを開き、次のコードを追加します。
-keep class com.google.** { public *; }
クラッシュ レポートを無効にする
Google Mobile Ads Unity Plugin は、デバッグと分析を目的としてクラッシュ レポートを収集します。クラッシュ レポートを無効にするには、Android と iOS の次のセクションをご覧ください。
Android
アプリの AndroidManifest.xml ファイルに、DISABLE_CRASH_REPORTING を true に設定した <meta-data> タグを追加します。
<manifest>
<application>
<meta-data
android:name="com.google.android.gms.ads.flag.DISABLE_CRASH_REPORTING"
android:value="true" />
</application>
</manifest>
iOS
DisableSDKCrashReporting メソッドを呼び出して、iOS でクラッシュ レポートを無効にします。
void Awake() {
MobileAds.DisableSDKCrashReporting();
}
Unity プラグインのバージョンを取得する
Unity SDK のバージョンを取得するには、次のコマンドを実行します。
// Get the Unity SDK version.
Debug.Log("Unity SDK Version: " + MobileAds.GetVersion());
プラットフォームのバージョンを取得する
Unity 用の Google Mobile Ads Unity Plugin は、Android プラットフォーム SDK と iOS プラットフォーム SDK に依存しています。プラットフォーム SDK のバージョンを取得するには、次のコマンドを実行します。
// Get the underlying platform SDK version.
Debug.Log("Platform SDK Version: " + MobileAds.GetPlatformVersion());