1
2

More than 1 year has passed since last update.

UnityのAdsで広告を追加する [2022]

Posted at

前回からの更新

前回、広告を実装してたら機能が追加されていて実装できなかったので、最新版で実装した際の記録を残します

開発環境

Unity 2021.3.9f1
Ads 4.3.0

前回からの変更点

・IsReadyの関数がなくなった
・Loadをする必要がある
・広告の種類が増えた(Interstitial、Rewarded、Banner)

Ads を有効にする

・[Window]-[General]-[Services]で開く
image.png

・Adsを開く
image.png

・Ads を ONにする
image.png

・最新版のパッケージをインストールする
image.png

・Test Mode にチェック
実機にビルドする場合はTest Modeのチェックを外します
(android ios の番号は後で使います)

image.png

・広告種類の確認。adsのダッシュボードのMonetizationのAd Unitsに表示されているidを確認します
https://unity.com/ja/products/unity-ads

image.png

・Advertisement.Initializeの部分は、先ほどadsをONにした際に表示されていたゲームidを設定する

using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class AdTest : MonoBehaviour
{
    const string ANDROID_INTERSTITIAL = "Interstitial_Android";
    const string ANDROID_REWARDED = "Rewarded_Android";
    const string ANDROID_BANNER = "Banner_Android";
    const string IOS_INTERSTITIAL = "Interstitial_iOS";
    const string IOS_REWARDED = "Rewarded_iOS";
    const string IOS_BANNER = "Banner_iOS";

    [SerializeField]
    public Button BtnAdd;

    private void Awake() {
        Advertisement.Banner.SetPosition(BannerPosition.TOP_CENTER);
#if UNITY_ANDROID
        Advertisement.Initialize("9999999");
        Advertisement.Load(ANDROID_INTERSTITIAL);
        Advertisement.Load(ANDROID_REWARDED);
        Advertisement.Load(ANDROID_BANNER);
        Advertisement.Banner.Show(ANDROID_BANNER);
        Advertisement.Show(ANDROID_INTERSTITIAL);
#elif UNITY_IOS
        Advertisement.Initialize("999999");
        Advertisement.Load(IOS_INTERSTITIAL);
        Advertisement.Load(IOS_REWARDED);
        Advertisement.Load(IOS_BANNER);
        Advertisement.Banner.Show(IOS_BANNER);
        Advertisement.Show(IOS_INTERSTITIA);
#endif
        BtnAdd.onClick.AddListener(() => {
#if UNITY_ANDROID
            Advertisement.Show(ANDROID_INTERSTITIAL);
#elif UNITY_IOS
        Advertisement.Show(IOS_INTERSTITIA);
#endif
        });
    }
}

・作成したクラスを適切なオブジェクトに追加し、ボタンを設定
image.png

・クリックした際、ダミーの画面が出ればOK
実機で確認する際は、テストモードの時は、Unity Remote 5の広告が表示され、テストモードを外すと本番用の広告が流れます
image.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2