Last active
July 1, 2020 02:22
-
-
Save yasuyuki-kamata/f2fcfb2f418affd201be4eddcc7f8967 to your computer and use it in GitHub Desktop.
Sample codes of UnityAdsSDK for Unity (C#)
This file contains 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 Mar 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 UnityEngine; | |
using UnityEngine.Advertisements; | |
public class UnityAdsExample : MonoBehaviour, IUnityAdsListener | |
{ | |
private string _gameId; | |
[SerializeField] private bool testMode; | |
[SerializeField] private string gameIdAppleAppStore = "3531391"; | |
[SerializeField] private string gameIdGooglePlay = "3531390"; | |
[SerializeField] private string placementId = "video"; | |
private void Start() | |
{ | |
InitAds(); | |
} | |
private void InitAds() | |
{ | |
if (!Advertisement.isSupported || Advertisement.isInitialized) return; | |
#if UNITY_ANDROID | |
_gameId = gameIdGooglePlay; | |
#elif UNITY_IOS | |
_gameId = gameIdAppleAppStore; | |
#endif | |
Advertisement.Initialize(_gameId, testMode); | |
Advertisement.AddListener(this); | |
} | |
public void ShowUnityAds() | |
{ | |
if (Advertisement.IsReady(placementId)) | |
{ | |
Advertisement.Show(placementId); | |
} | |
} | |
public void OnUnityAdsReady(string placementId) | |
{ | |
Debug.Log($"OnUnityAdsReady with placement id: {placementId}"); | |
} | |
public void OnUnityAdsDidError(string message) | |
{ | |
Debug.Log($"OnUnityAdsDidError with message: {message}"); | |
} | |
public void OnUnityAdsDidStart(string placementId) | |
{ | |
Debug.Log($"OnUnityAdsDidStart with placement id: {placementId}"); | |
} | |
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult) | |
{ | |
Debug.Log($"OnUnityAdsDidFinish with placement id: {placementId} and showResult: {showResult}"); | |
} | |
public void OnDestroy() | |
{ | |
Advertisement.RemoveListener(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment