「Unity/GoogleMobileAds/アプリ起動広告」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==アプリ起動広告とは== アプリ起動時に表示される広告 UnityAdMobのv.6.1.0以降で使える。 ==サンプル== OpenScene.cs <pre> public class Op...」) |
|||
行19: | 行19: | ||
if (!paused) | if (!paused) | ||
{ | { | ||
− | + | 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.Show(); | ad.Show(); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
} | } |
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(); } }