Last active
May 29, 2017 10:09
-
-
Save keiranlovett/c5b768c6f45bbbea301a6c89d2bc38ae to your computer and use it in GitHub Desktop.
Simple Unity Ads Integration with Callbacks
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
| UnityAds.ShowRewardedAd("AdRewards", delegate(bool result) | |
| { | |
| if (result) | |
| { | |
| //Reward | |
| } | |
| else | |
| { | |
| //Message? | |
| } | |
| }); |
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
| 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