全般設定

MobileAds クラスは、Google Mobile Ads Unity Plugin のグローバル設定を提供します。

Unity のメインスレッドで広告イベントを発生させる

Google Mobile Ads Unity Plugin は、Unity メインスレッドとは別のスレッドでイベントを発生させます。広告イベントを実装して Unity オブジェクトを操作する場合は、Google Mobile Ads Unity Plugin イベントを Unity のメインスレッドと同期する必要があります。

Google Mobile Ads Unity Plugin に同期を任せることで、広告イベントを Unity のメインスレッドと手動または自動で同期します。

推奨: 広告イベントを手動で同期する

広告イベントを手動で同期するには、メインスレッドで 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(デバイスの現在の音量)に設定されており、アプリはミュートされていない状態です。

アプリに特別な要件がある場合は、オプションの ApplicationPreferences キー gad_has_consent_for_cookies を 0 に設定して、制限付き広告を有効にできます。

// Enable limited ads
ApplicationPreferences.SetInt("gad_has_consent_for_cookies", 0);

Android の縮小化

この Unity パブリッシュ オプションを使用すると、Java コードの最小化を有効にできます。縮小を有効にする場合は、SDK で参照されるクラスを保持するためのカスタム ProGuard ファイルも作成する必要があります。

  1. カスタム Proguard ファイルを有効にする

    [Project Settings](プロジェクト設定)> [Player](プレーヤー)> [Android] > [Publishing Settings](公開設定)> [Build](ビルド)に移動し、以下を選択します。

    • カスタム Proguard ファイル
  2. /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

iOS でクラッシュ レポートを無効にするには、DisableSDKCrashReporting メソッドを呼び出します。

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 と iOS のプラットフォーム SDK に依存しています。プラットフォーム SDK のバージョンを取得するには、次のコマンドを実行します。

// Get the underlying platform SDK version.
Debug.Log("Platform SDK Version: " + MobileAds.GetPlatformVersion());