facebook twitter hatena line email

Unity/GoogleMobileAds/アプリ起動広告

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

アプリ起動広告とは

アプリ起動時に表示される広告

UnityAdMobのv.6.1.0以降で使える。

サンプル

OpenScene.cs

public class OpenScene : AbstractScene
{
    void Start()
    {
        AdMobOpenManager.Instance.Init();
        AdMobOpenManager.Instance.LoadAd();
    }
    public void OnApplicationPause(bool paused)
    {
        // Display the app open ad when the app is foregrounded
        if (!paused)
        {
            AdMobOpenManager.Instance.ShowAdIfAvailable();
        }
    }
}

AdMobOpenManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using GoogleMobileAds.Api;

public class AdMobOpenManager
{
    private static AdMobOpenManager instance;
    AdRequest request;
    private AppOpenAd ad;
    private bool isShowingAd = false;
    private DateTime loadTime;
    public static AdMobOpenManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new AdMobOpenManager();
            }
            return instance;
        }
    }

    private bool IsAdAvailable
    {
        get
        {
            return ad != null && (System.DateTime.UtcNow - loadTime).TotalHours < 4;
        }
    }

    public void Init()
    {
        request = new AdRequest.Builder()
            .Build();
    }
    public void LoadAd()
    {
        if (IsAdAvailable)
        {
            return;
        }
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/3419835294";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3940256099942544/5662855259";
#else
        string adUnitId = "unexpected_platform";
#endif
        AppOpenAd.Load(GetAdUnitId(), request, (appOpenAdd, loadAdError) =>
        {
            if (loadAdError != null)
            {
                Debug.LogFormat("Failed to load the ad. (reason: {0})", loadAdError.GetMessage());
                return;
            }

            ad = appOpenAdd;
            loadTime = DateTime.UtcNow;
            RegisterEventHandlers(appOpenAdd);
            ShowAdIfAvailable();
        }));
    }

    private void RegisterEventHandlers(AppOpenAd ad)
    {
        // Raised when the ad is estimated to have earned money.
        ad.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log(String.Format("Openad paid {0} {1}.",
                adValue.Value,
                adValue.CurrencyCode));

        };
        // Raised when an impression is recorded for an ad.
        ad.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Openad recorded an impression.");
        };
        // Raised when a click is recorded for an ad.
        ad.OnAdClicked += () =>
        {
            Debug.Log("Openad was clicked.");
        };
        // Raised when an ad opened full screen content.
        ad.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Openad full screen content opened.");
            isShowingAd = true;
        };
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += () =>
        {
            Debug.Log("Openad full screen content closed.");
        };
        // Raised when the ad failed to open full screen content.
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Openad failed to open full screen content " +
                           "with error : " + error);
        };
    }
    public void ShowAdIfAvailable()
    {
        if (!IsAdAvailable || isShowingAd)
        {
            return;
        }
        ad.Show();
    }
}

参考:https://codelabs.developers.google.com/codelabs/admob-appopen-unity?hl=ja#4

AppOpenAd.Loadが、CS0618の警告を出す件の修正方法

警告詳細

warning CS0618: 'AppOpenAd.Load(string, ScreenOrientation, AdRequest, Action<AppOpenAd, LoadAdError>)' is obsolete: 'Use Load(string, AdRequest, Action<AppOpenAd, LoadAdError>) instead.'

Loadを、以下のように修正

AppOpenAd.Load(GetAdUnitId(), request, (appOpenAdd, loadAdError) =>
{
}));