Skip to content

Instantly share code, notes, and snippets.

@jcguarinpenaranda
Created August 8, 2015 22:41
Show Gist options
  • Save jcguarinpenaranda/7ca8c1aa271621d0a071 to your computer and use it in GitHub Desktop.
Save jcguarinpenaranda/7ca8c1aa271621d0a071 to your computer and use it in GitHub Desktop.
AdManager is a script copied from Unity Technologies which allows you to add Ads integration very easy
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class AdManager : MonoBehaviour
{
//Important: Change the gameID to your game's
//id in the unityads.unity3d.com dashboard
[SerializeField] string gameID = "33675";
void Awake()
{
Advertisement.Initialize (gameID, true);
}
public void ShowAd(string zone = "")
{
//We simulate the mobile's behaviour.
//It results to be that in the editor, showing
//the ad doesn't stop the game for playing
//in the background.
#if UNITY_EDITOR
StartCoroutine(WaitForAd ());
#endif
if (string.Equals (zone, ""))
zone = null;
ShowOptions options = new ShowOptions ();
options.resultCallback = AdCallbackhandler;
if (Advertisement.isReady (zone))
Advertisement.Show (zone, options);
}
void AdCallbackhandler (ShowResult result)
{
switch(result)
{
case ShowResult.Finished:
Debug.Log ("Ad Finished. Rewarding player...");
break;
case ShowResult.Skipped:
Debug.Log ("Ad skipped. Son, I am dissapointed in you");
break;
case ShowResult.Failed:
Debug.Log("I swear this has never happened to me before");
break;
}
}
IEnumerator WaitForAd()
{
float currentTimeScale = Time.timeScale;
Time.timeScale = 0f;
yield return null;
while (Advertisement.isShowing)
yield return null;
Time.timeScale = currentTimeScale;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment