Last active
July 24, 2022 13:28
-
-
Save yasuyuki-kamata/3efd44994b16f4b398b136290ba9379d to your computer and use it in GitHub Desktop.
Unity Mediationでインタースティシャル広告を表示するサンプル - UNIBOOK14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Unity.Services.Core; | |
using Unity.Services.Mediation; | |
using UnityEngine; | |
namespace Unity.Example | |
{ | |
public class InterstitialAdExample : MonoBehaviour | |
{ | |
private IInterstitialAd _ad; | |
private string _adUnitId; | |
private string _gameId; | |
[SerializeField] | |
private string adUnitId_iOS = "Interstitial_iOS"; | |
[SerializeField] | |
private string adUnitId_Android = "Interstitial_Android"; | |
[SerializeField] | |
private string gameId_iOS = "4846192"; | |
[SerializeField] | |
private string gameId_Android = "4846193"; | |
/// <summary> | |
/// サービスの初期化 | |
/// </summary> | |
public async void InitServices() | |
{ | |
try | |
{ | |
// プラットフォームごとのIDをセット | |
#if UNITY_IOS | |
_adUnitId = adUnitId_iOS; | |
_gameId = gameId_iOS; | |
#else | |
_adUnitId = adUnitId_Android; | |
_gameId = gameId_Android; | |
#endif | |
// 初期化オプション設定 | |
var initializationOptions = new InitializationOptions(); | |
// 使用するGame IDを指定 | |
initializationOptions.SetGameId(_gameId); | |
// 初期化 | |
await UnityServices.InitializeAsync(initializationOptions); | |
InitializationComplete(); | |
} | |
catch (Exception e) | |
{ | |
InitializationFailed(e); | |
} | |
} | |
/// <summary> | |
/// 広告のセットアップ | |
/// </summary> | |
public void SetupAd() | |
{ | |
// インタースティシャル広告のインスタンスを生成 | |
_ad = MediationService.Instance.CreateInterstitialAd(_adUnitId); | |
// イベントを登録 | |
_ad.OnClosed += AdClosed; | |
_ad.OnClicked += AdClicked; | |
_ad.OnLoaded += AdLoaded; | |
_ad.OnFailedLoad += AdFailedLoad; | |
// インプレッションイベントを登録 | |
MediationService.Instance.ImpressionEventPublisher | |
.OnImpression += ImpressionEvent; | |
} | |
/// <summary> | |
/// 広告を表示する | |
/// </summary> | |
public async void ShowAd() | |
{ | |
// 広告コンテンツがロードされているかチェック | |
if (_ad.AdState == AdState.Loaded) | |
{ | |
try | |
{ | |
// 広告の表示オプション設定 | |
var showOptions = new InterstitialAdShowOptions(); | |
// 自動リロードをON | |
showOptions.AutoReload = true; | |
// 広告表示 | |
await _ad.ShowAsync(showOptions); | |
AdShown(); | |
} | |
catch (ShowFailedException e) | |
{ | |
AdFailedShow(e); | |
} | |
} | |
} | |
private void OnDestroy() | |
{ | |
if (_ad is not null) | |
{ | |
// イベントの削除 | |
_ad.OnClosed -= AdClosed; | |
_ad.OnClicked -= AdClicked; | |
_ad.OnLoaded -= AdLoaded; | |
_ad.OnFailedLoad -= AdFailedLoad; | |
} | |
} | |
/// <summary> | |
/// 初期化完了時の処理 | |
/// </summary> | |
void InitializationComplete() | |
{ | |
SetupAd(); | |
LoadAd(); | |
} | |
/// <summary> | |
/// 広告のロード | |
/// </summary> | |
async void LoadAd() | |
{ | |
try | |
{ | |
await _ad.LoadAsync(); | |
} | |
catch (LoadFailedException) | |
{ | |
// 広告ロードの失敗はOnFailedLoadコールバックで処理する | |
} | |
} | |
/// <summary> | |
/// 初期化失敗時の処理 | |
/// </summary> | |
void InitializationFailed(Exception e) | |
{ | |
Debug.Log("Initialization Failed: " + e.Message); | |
} | |
/// <summary> | |
/// 広告ロード完了時の処理 | |
/// </summary> | |
void AdLoaded(object sender, EventArgs e) | |
{ | |
Debug.Log("Ad loaded"); | |
} | |
/// <summary> | |
/// 広告ロード失敗時の処理 | |
/// </summary> | |
void AdFailedLoad(object sender, LoadErrorEventArgs e) | |
{ | |
Debug.Log("Failed to load ad"); | |
Debug.Log(e.Message); | |
} | |
/// <summary> | |
/// 広告表示完了時の処理 | |
/// </summary> | |
void AdShown() | |
{ | |
Debug.Log("Ad shown!"); | |
} | |
/// <summary> | |
/// 広告クローズ時の処理 | |
/// </summary> | |
void AdClosed(object sender, EventArgs e) | |
{ | |
Debug.Log("Ad has closed"); | |
} | |
/// <summary> | |
/// 広告クリック時の処理 | |
/// </summary> | |
void AdClicked(object sender, EventArgs e) | |
{ | |
Debug.Log("Ad has been clicked"); | |
} | |
/// <summary> | |
/// 広告表示失敗時の処理 | |
/// </summary> | |
void AdFailedShow(ShowFailedException e) | |
{ | |
Debug.Log(e.Message); | |
} | |
/// <summary> | |
/// 広告インプレッション時の処理 | |
/// </summary> | |
void ImpressionEvent(object sender, ImpressionEventArgs args) | |
{ | |
var impressionData = args.ImpressionData != null ? | |
JsonUtility.ToJson(args.ImpressionData, true) : | |
"null"; | |
Debug.Log($"インプレッションイベント:{args.AdUnitId} {impressionData}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment