Last active
August 29, 2015 14:26
-
-
Save talenguyen/bfe996cd3c813197a11a to your computer and use it in GitHub Desktop.
AdMob implement for display Google Admob in Unity project. NOTE: MonoSingleton is open source file on Github. and IAd is my interface for ad provider
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 System.Collections; | |
using GoogleMobileAds.Api; | |
using System; | |
using GoogleMobileAds; | |
public class AdMob : MonoSingleton<AdMob>, IAd | |
{ | |
[Header("Config ad id")] | |
[SerializeField] | |
private string | |
iOSInterstitial; | |
[SerializeField] | |
private string | |
androidInterstitial; | |
[Header("Add list of test device which use for test purpose and only display test ad.")] | |
[SerializeField] | |
private string[] | |
testDevices; | |
[Header("Handle ad fail to load.")] | |
[SerializeField] | |
private int | |
retryIfFail = 0; | |
private int retry = 0; | |
private InterstitialAd interstitial; | |
public void prepareFullscreenAd () | |
{ | |
if (retry > 0) { | |
// Retrying to load then we do nothing. | |
return; | |
} | |
requestNewInterstitialAd (); | |
} | |
public void showFullsreenAd () | |
{ | |
if (interstitial != null && interstitial.IsLoaded ()) { | |
interstitial.Show (); | |
} | |
} | |
private void requestNewInterstitialAd () | |
{ | |
#if UNITY_EDITOR | |
string adUnitId = "unused"; | |
#elif UNITY_ANDROID | |
string adUnitId = androidInterstitial; | |
#elif UNITY_IPHONE | |
string adUnitId = iOSInterstitial; | |
#else | |
string adUnitId = "unexpected_platform"; | |
#endif | |
// Create an interstitial. | |
interstitial = new InterstitialAd (adUnitId); | |
// Register for ad events. | |
interstitial.AdLoaded += HandleInterstitialLoaded; | |
interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad; | |
// Load an interstitial ad. | |
interstitial.LoadAd (createAdRequest ()); | |
} | |
// Returns an ad request with custom ad targeting. | |
private AdRequest createAdRequest () | |
{ | |
AdRequest.Builder builder = new AdRequest.Builder (); | |
if (testDevices != null && testDevices.Length > 0) { | |
for (int i = 0; i < testDevices.Length; i++) { | |
builder.AddTestDevice (testDevices [i]); | |
} | |
} | |
return builder.Build (); | |
} | |
#region Interstitial callback handlers | |
public void HandleInterstitialLoaded (object sender, EventArgs args) | |
{ | |
print ("HandleInterstitialLoaded event received."); | |
retry = 0; | |
} | |
public void HandleInterstitialFailedToLoad (object sender, AdFailedToLoadEventArgs args) | |
{ | |
Debug.Log ("HandleInterstitialFailedToLoad event received with message: " + args.Message); | |
if (retry < retryIfFail) { | |
requestNewInterstitialAd (); | |
retry++; | |
} else { | |
retry = 0; | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment