Skip to content

Instantly share code, notes, and snippets.

@keiranlovett
Last active May 29, 2017 10:09
Show Gist options
  • Select an option

  • Save keiranlovett/c5b768c6f45bbbea301a6c89d2bc38ae to your computer and use it in GitHub Desktop.

Select an option

Save keiranlovett/c5b768c6f45bbbea301a6c89d2bc38ae to your computer and use it in GitHub Desktop.
Simple Unity Ads Integration with Callbacks
UnityAds.ShowRewardedAd("AdRewards", delegate(bool result)
{
if (result)
{
//Reward
}
else
{
//Message?
}
});
using UnityEngine;
using UnityEngine.Advertisements;
public class UnityAds : MonoBehaviour
{
private static UnityAds _instance;
public static UnityAds Instance
{
get
{
if ( _instance == null )
{
_instance = FindObjectOfType<UnityAds>();
}
return _instance;
}
}
void Awake() {
Advertisement.Initialize("~~~~~YOURGAMEIDHERE~~~~~", true); // ...initialize.
}
public static void ShowRewardedAd(string adIdentifierString, System.Action<bool> onVideoPlayed)
{
Debug.Log(string.Format("Platform is {0}supported\nUnity Ads {1}initialized",
Advertisement.isSupported ? "" : "not ", Advertisement.isInitialized ? "" : "not "));
if (Advertisement.IsReady (adIdentifierString)) {
Advertisement.Show (adIdentifierString, new ShowOptions {
resultCallback = result => {
switch (result)
{
case (ShowResult.Finished):
Debug.Log("The ad was successfully shown.");
//
// YOUR CODE TO REWARD THE GAMER
// Give coins etc.
onVideoPlayed(true);
break;
case (ShowResult.Skipped):
Debug.Log("The ad was skipped before reaching the end.");
onVideoPlayed(false);
break;
case (ShowResult.Failed):
Debug.LogError("The ad failed to be shown.");
onVideoPlayed(false);
break;
}
}
});
}
onVideoPlayed(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment