facebook twitter hatena line email

Unity/GoogleMobileAds/アプリ起動広告

提供: 初心者エンジニアの簡易メモ
2022年4月17日 (日) 00:27時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「==アプリ起動広告とは== アプリ起動時に表示される広告 UnityAdMobのv.6.1.0以降で使える。 ==サンプル== OpenScene.cs <pre> public class Op...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

アプリ起動広告とは

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

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.LoadAd(adUnitId, ScreenOrientation.Portrait, request, ((appOpenAd, error) =>
        {
            if (error != null)
            {
                Debug.LogFormat("Failed to load the ad. (reason: {0})", error.LoadAdError.GetMessage());
                return;
            }

            ad = appOpenAd;
            loadTime = DateTime.UtcNow;
        }));
    }

    public void ShowAdIfAvailable()
    {
        if (!IsAdAvailable || isShowingAd)
        {
            return;
        }

        ad.OnAdDidDismissFullScreenContent += HandleAdDidDismissFullScreenContent;
        ad.OnAdFailedToPresentFullScreenContent += HandleAdFailedToPresentFullScreenContent;
        ad.OnAdDidPresentFullScreenContent += HandleAdDidPresentFullScreenContent;
        ad.OnAdDidRecordImpression += HandleAdDidRecordImpression;
        ad.OnPaidEvent += HandlePaidEvent;

        ad.Show();
    }
    private void HandleAdDidDismissFullScreenContent(object sender, EventArgs args)
    {
        Debug.Log("Closed app open ad");
        // Set the ad to null to indicate that AppOpenAdManager no longer has another ad to show.
        ad = null;
        isShowingAd = false;
        LoadAd();
    }

    private void HandleAdFailedToPresentFullScreenContent(object sender, AdErrorEventArgs args)
    {
        Debug.LogFormat("Failed to present the ad (reason: {0})", args.AdError.GetMessage());
        // Set the ad to null to indicate that AppOpenAdManager no longer has another ad to show.
        ad = null;
        LoadAd();
    }

    private void HandleAdDidPresentFullScreenContent(object sender, EventArgs args)
    {
        Debug.Log("Displayed app open ad");
        isShowingAd = true;
    }

    private void HandleAdDidRecordImpression(object sender, EventArgs args)
    {
        Debug.Log("Recorded ad impression");
    }

    private void HandlePaidEvent(object sender, AdValueEventArgs args)
    {
        Debug.LogFormat("Received paid event. (currency: {0}, value: {1}",
                args.AdValue.CurrencyCode, args.AdValue.Value);
    }
}

公式:https://developers.google.com/admob/ios/app-open