facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==アプリ起動広告とは== アプリ起動時に表示される広告 UnityAdMobのv.6.1.0以降で使える。 ==サンプル== OpenScene.cs <pre> public class Op...」)
 
(AppOpenAd.Loadが、CS0618の警告を出す)
 
(同じ利用者による、間の6版が非表示)
行19: 行19:
 
         if (!paused)
 
         if (!paused)
 
         {
 
         {
        AdMobOpenManager.Instance.ShowAdIfAvailable();
+
            AdMobOpenManager.Instance.ShowAdIfAvailable();
 
         }
 
         }
 
     }
 
     }
行77: 行77:
 
         string adUnitId = "unexpected_platform";
 
         string adUnitId = "unexpected_platform";
 
#endif
 
#endif
         AppOpenAd.LoadAd(adUnitId, ScreenOrientation.Portrait, request, ((appOpenAd, error) =>
+
         AppOpenAd.Load(GetAdUnitId(), request, (appOpenAdd, loadAdError) =>
 
         {
 
         {
             if (error != null)
+
             if (loadAdError != null)
 
             {
 
             {
                 Debug.LogFormat("Failed to load the ad. (reason: {0})", error.LoadAdError.GetMessage());
+
                 Debug.LogFormat("Failed to load the ad. (reason: {0})", loadAdError.GetMessage());
 
                 return;
 
                 return;
 
             }
 
             }
  
             ad = appOpenAd;
+
             ad = appOpenAdd;
 
             loadTime = DateTime.UtcNow;
 
             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()
 
     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)
+
}
    {
+
</pre>
        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)
+
参考:https://codelabs.developers.google.com/codelabs/admob-appopen-unity?hl=ja#4
    {
+
        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)
+
==AppOpenAd.Loadが、CS0618の警告を出す件の修正方法==
    {
+
警告詳細
        Debug.Log("Displayed app open ad");
+
warning CS0618: 'AppOpenAd.Load(string, ScreenOrientation, AdRequest, Action<AppOpenAd, LoadAdError>)' is obsolete: 'Use Load(string, AdRequest, Action<AppOpenAd, LoadAdError>) instead.'
        isShowingAd = true;
+
    }
+
  
    private void HandleAdDidRecordImpression(object sender, EventArgs args)
+
Loadを、以下のように修正
    {
+
<pre>
        Debug.Log("Recorded ad impression");
+
AppOpenAd.Load(GetAdUnitId(), request, (appOpenAdd, loadAdError) =>
    }
+
{
 
+
}));
    private void HandlePaidEvent(object sender, AdValueEventArgs args)
+
    {
+
        Debug.LogFormat("Received paid event. (currency: {0}, value: {1}",
+
                args.AdValue.CurrencyCode, args.AdValue.Value);
+
    }
+
}
+
 
</pre>
 
</pre>
 
公式:https://developers.google.com/admob/ios/app-open
 

2024年1月2日 (火) 04:22時点における最新版

アプリ起動広告とは

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

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) =>
{
}));