Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample codes of UnityAdsSDK for Unity (C#)
/// 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