unity ads tutorial 2021 code example
Example 1: unity ads tutorial 2020
using UnityEngine;
using UnityEngine.Advertisements;
public class InterstitialAdsScript : MonoBehaviour {
#if UNITY_IOS
private string gameId = "1486551";
#elif UNITY_ANDROID
private string gameId = "1486550";
#endif
bool testMode = true;
void Start () {
Advertisement.Initialize (gameId, testMode);
Advertisement.Show ();
}
}
Example 2: unity ads tutorial 2020
using UnityEngine;
using UnityEngine.Advertisements;
public class RewardedAdsScript : MonoBehaviour, IUnityAdsListener {
#if UNITY_IOS
private string gameId = "1486551";
#elif UNITY_ANDROID
private string gameId = "1486550";
#endif
string myPlacementId = "rewardedVideo";
bool testMode = true;
void Start () {
Advertisement.AddListener (this);
Advertisement.Initialize (gameId, testMode);
}
public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
if (showResult == ShowResult.Finished) {
} else if (showResult == ShowResult.Skipped) {
} else if (showResult == ShowResult.Failed) {
Debug.LogWarning ("The ad did not finish due to an error.");
}
}
public void OnUnityAdsReady (string placementId) {
if (placementId == myPlacementId) {
Advertisement.Show (myPlacementId);
}
}
public void OnUnityAdsDidError (string message) {
}
public void OnUnityAdsDidStart (string placementId) {
}
}