LoginSignup
3
3

More than 1 year has passed since last update.

【Unity】Unityのダッシュボードのレイアウト変わってたから実装手順は俺が書く

Last updated at Posted at 2021-10-15

Unityダッシュボードのレイアウト変わっててUnityAdsの導入手順が分からない

掲題の通りだ。金が欲しいのでUnityでゲーム作って広告入れようと思ったら、ダッシュボードのレイアウトが大きく変わったため既存の導入手順系の記事通りに行かなくなってしまった。
「いや公式リファレンス読めばわかるじゃんwww」という方は帰ってどうぞ。君らはターゲットじゃない。
同じことで悩んでいる初学者のみんな、頑張りましょう。公式リファレンスは俺が代わりに読んどくよ。

内容

  • 一応、公式リファレンスがどこから見られるか書いとくよ   以下を参考にしてね※Unity用のページの開き方だよ
    ダッシュボードのスクショ.png ダッシュボードのスクショ2.png

これでIntegration guide for Unityってページが開くよ。

ダッシュボード画面のガイドはこっち

あとはこのリファレンス通りに作業するだけだから、英語強い人は上のサイト見てね。

準備するよ

  • ビルドターゲットを変更
    ここではAndroidにするよ。UnityEditor左上の[File]>[Build Settings]を押すよ

    image.png

    右下に[Switch Platform]ってボタンが出るはずなので、それを押すよ

    image.png

  • UnityAdsをインストールするよ
    UnityEditor左上の[Window]>[PackageManager]を押してね
    image.png

  • AdvertisementをImportするよ
    こんな感じの画面が出るから、Advertisementを探してね。右下(この画像だと[Up to date]ボタンの位置)に[Install]ってボタンがあるから押してね。なんか[Import]みたいな画面でるはずだから、色々先に進みそうなボタン押してね。
    image.png

  • ダッシュボードで広告の設定するよ
    ここ押すよ
    image.png

最初どんな画面が出るか忘れちゃった。何かしらやって、以下の画面になったら[Add Ad Unit]を押してね

ダッシュボードのスクショ4.png

こんな画面になるから、適宜情報を入力しよう。
image.png
- [Rewarded]はスキップできない広告で、見終わるとプレイヤーにゲーム内通貨とかの報酬を与えられるやつ。漫画アプリとかで広告みたら一話読めたりするよね。
- [Interstitial]は一定秒数経過で広告をスキップできる。基本的にはこっち選んどこうね。面倒くさいからね。

こんなんでるよ
image.png

[+Advanced Settings]で表示する広告について音出すかとか、何秒で飛ばせるかとか決められるみたい。決定ボタンみたいなのはないから、設定終わったら[←]を押してひとつ前の画面に戻る
ダッシュボードのスクショ5.png

そしたら一番下にこんな感じで今作った広告の設定?が表示されるはず
ダッシュボードのスクショ6.png
もっと詳しく設定したい人はこっち

組み込んでいくよ

じゃあそろそろゲームの方に組み込んでいこう。

まずGameIDを調べよう。ダッシュボード上で、さっき開いてたAd Unitsのページをもう一回開いてね。
ここにApple用のGameIDとAndroid用のGameID載ってるから、控えといてね。

ダッシュボードのスクショ7.png

したらScene内に空のGameObject置くか、ゲーム開始時点~ゲーム中も存在するObjectに以下の内容のScriptをアタッチしてあげてね。
※リファレンスからの丸ごとコピーだから、もし権利者に怒られたら消すよ

using UnityEngine;
using UnityEngine.Advertisements;

public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
{
    [SerializeField] string _androidGameId;
    [SerializeField] string _iOsGameId;
    [SerializeField] bool _testMode = true;
    [SerializeField] bool _enablePerPlacementMode = true;
    private string _gameId;

    void Awake()
    {
        InitializeAds();
    }

    public void InitializeAds()
    {
        _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOsGameId
            : _androidGameId;
        Advertisement.Initialize(_gameId, _testMode, _enablePerPlacementMode, this);
    }

    public void OnInitializationComplete()
    {
        Debug.Log("Unity Ads initialization complete.");
    }

    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    }
}

InspectorからAndroid Game Id, iOsGameIdのとこに、さっき控えたandroidのGameIDとiOSのGameIDを入力しよう

エディタのスクショ.png

ここまでで広告表示のためのSDK初期化ができたよ。あと少し!
さっきのスクリプトと同じGameObjectに以下のScriptをアタッチしてあげてね。

using UnityEngine;
using UnityEngine.Advertisements;

public class InterstitialAdExample : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
    [SerializeField] string _androidAdUnitId = "Interstitial_Android";
    [SerializeField] string _iOsAdUnitId = "Interstitial_iOS";
    string _adUnitId;

    void Awake()
    {
        // Get the Ad Unit ID for the current platform:
        _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
            ? _iOsAdUnitId
            : _androidAdUnitId;
    }

    // Load content to the Ad Unit:
    public void LoadAd()
    {
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
        Debug.Log("Loading Ad: " + _adUnitId);
        Advertisement.Load(_adUnitId, this);
    }

    // Show the loaded content in the Ad Unit: 
    public void ShowAd()
    {
        // Note that if the ad content wasn't previously loaded, this method will fail
        Debug.Log("Showing Ad: " + _adUnitId);
        Advertisement.Show(_adUnitId, this);
    }

    // Implement Load Listener and Show Listener interface methods:  
    public void OnUnityAdsAdLoaded(string adUnitId)
    {
        // Optionally execute code if the Ad Unit successfully loads content.
    }

    public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    {
        Debug.Log($"Error loading Ad Unit: {adUnitId} - {error.ToString()} - {message}");
        // Optionally execite code if the Ad Unit fails to load, such as attempting to try again.
    }

    public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
        // Optionally execite code if the Ad Unit fails to show, such as loading another ad.
    }

    public void OnUnityAdsShowStart(string adUnitId) { }
    public void OnUnityAdsShowClick(string adUnitId) { }
    public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState) { }

    public void OnGUI()
    {
        if (GUILayout.Button("LoadAndShowAd"))
        {
            LoadAd();
            ShowAd();
        }
    }
}

これでゲームを再生してみてね。画面上方に以下みたいな感じで[LoadAndShowAd]ってボタンが出てくるから、これを押してみてね。
image.png
image.png

広告が表示されると思う。されなかった?困っちゃうね。(投げやり)
image.png

たぶんね、Inspector上でInterstitialAdExampleのAndroid Ad Unit Idを、さっき作った広告設定のIDに書き換えるとそれを表示するようにできるんですよ。

でもね、上述の手順だけだとその作成した広告設定の情報が穴だらけで使えないみたい。だからそこは自分で調べてみてね。
僕も調べるから、また進捗あれば更新するよ。

以上

想像以上に雑な内容でビビったかな?ごめんね、でも英語分からなくても広告が出せるとこまではたぶんいったよね?それで許してね。
今後も僕自身が「これ調べても出てこねーじゃんかよォ!!!!」て激怒することがあれば、こんな感じでHowTo記事にしようと思うよ。じゃあの。

追記

  • このままビルドするとテスト広告が表示されてしまうよ
    作ってる最中の確認はテスト広告でいいけど、ストアで売り出すときはこのままだとUnityAdsのテスト広告が流れちゃうよ。
    スクリプト上でいじってもいいけど、手っ取り早いのはInspector上でAdsInitializerTestModeのチェックボックスを外すことだよ。
    テスト広告のチェックを外す.png
    金を稼ぐ下準備はできたね!そろそろゲーム作ろうか。
3
3
2

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
3
3