Last active
August 31, 2020 10:51
-
-
Save yasuyuki-kamata/477c91e360562bddbb28d8cdcbbdf47b to your computer and use it in GitHub Desktop.
UnityAdsでリワード広告を表示するサンプルコード(C#)
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
/// UnityAdsExample.cs | |
/// Created by Yasuyuki Kamata on 31 Aug 2020 | |
/// Copyright © 2020 Yasuyuki Kamata. Licensed under MIT License | |
/// | |
/// <summary> | |
/// Unity Ads Example using Advertisements namespace | |
/// (Unity Ads SDK 3.4.x or later) | |
/// </summary> | |
using System; | |
using UnityEngine; | |
using UnityEngine.Advertisements; | |
public class UnityAdsExampleScript : MonoBehaviour, IUnityAdsListener | |
{ | |
public string gameIdAppleAppStore = "2907766"; // Apple AppStore用のGame ID | |
public string gameIdGooglePlay = "2907765"; // GooglePlay用のGame ID | |
public string rewardedPlacementId = "rewardedVideo"; // リワード広告枠ID | |
public bool testMode; | |
private string _gameId; | |
void Start() | |
{ | |
InitUnityAds(); | |
} | |
private void InitUnityAds() | |
{ | |
if (!Advertisement.isSupported || Advertisement.isInitialized) return; | |
#if UNITY_IOS | |
_gameId = gameIdAppleAppStore; | |
#elif UNITY_ANDROID | |
_gameId = gameIdGooglePlay; | |
#endif | |
// Unity Adsの初期化 | |
Advertisement.Initialize(_gameId, testMode); | |
// コールバックリスナーの登録 | |
Advertisement.AddListener(this); | |
} | |
public void ShowRewardedAds() | |
{ | |
// 指定の広告枠の準備ができていなかったら終了 | |
if (!Advertisement.IsReady(rewardedPlacementId)) return; | |
// 指定の広告枠を表示する | |
Advertisement.Show(rewardedPlacementId); | |
} | |
public void OnUnityAdsReady(string placementId) | |
{ | |
// ここに広告の準備が完了したときの処理 | |
} | |
public void OnUnityAdsDidError(string message) | |
{ | |
// ここにエラーを受け取ったときの処理 | |
} | |
public void OnUnityAdsDidStart(string placementId) | |
{ | |
// ここに広告表示を開始したときの処理 | |
} | |
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) | |
{ | |
// ここに広告表示が完了したときの処理 | |
switch (showResult) | |
{ | |
case ShowResult.Finished: | |
// 広告視聴を完了 | |
break; | |
case ShowResult.Failed: | |
// 広告視聴に失敗 | |
break; | |
case ShowResult.Skipped: | |
// 広告をスキップした | |
break; | |
default: | |
throw new ArgumentOutOfRangeException( | |
nameof(showResult), | |
showResult, | |
null); | |
} | |
} | |
void OnDestroy() | |
{ | |
Advertisement.RemoveListener(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment