facebook twitter hatena line email

「Unity/GoogleMobileAds/アプリ起動広告」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==アプリ起動広告とは== アプリ起動時に表示される広告 UnityAdMobのv.6.1.0以降で使える。 ==サンプル== OpenScene.cs <pre> public class Op...」)
 
行19: 行19:
 
         if (!paused)
 
         if (!paused)
 
         {
 
         {
        AdMobOpenManager.Instance.ShowAdIfAvailable();
+
            AdMobOpenManager.Instance.ShowAdIfAvailable();
 
         }
 
         }
 
     }
 
     }
行87: 行87:
 
             ad = appOpenAd;
 
             ad = appOpenAd;
 
             loadTime = DateTime.UtcNow;
 
             loadTime = DateTime.UtcNow;
 +
            RegisterEventHandlers(ad);
 +
            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()
 
     public void ShowAdIfAvailable()
 
     {
 
     {
行96: 行136:
 
             return;
 
             return;
 
         }
 
         }
 
        ad.OnAdDidDismissFullScreenContent += HandleAdDidDismissFullScreenContent;
 
        ad.OnAdFailedToPresentFullScreenContent += HandleAdFailedToPresentFullScreenContent;
 
        ad.OnAdDidPresentFullScreenContent += HandleAdDidPresentFullScreenContent;
 
        ad.OnAdDidRecordImpression += HandleAdDidRecordImpression;
 
        ad.OnPaidEvent += HandlePaidEvent;
 
 
 
         ad.Show();
 
         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);
 
 
     }
 
     }
 
}
 
}

2023年12月30日 (土) 02:07時点における版

アプリ起動広告とは

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

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;
            RegisterEventHandlers(ad);
            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://developers.google.com/admob/ios/app-open