Skip to content

Instantly share code, notes, and snippets.

@rtroe
Last active March 17, 2018 00:38
Show Gist options
  • Save rtroe/e6e01921e8e9d7cfbe63160d7aa1e628 to your computer and use it in GitHub Desktop.
Save rtroe/e6e01921e8e9d7cfbe63160d7aa1e628 to your computer and use it in GitHub Desktop.
/// <summary>
/// Adds the banner.
/// </summary>
/// <param name="adUnitID">Ad unit identifier.</param>
/// <param name="Location">Location.</param>
public void InitBanner(string adUnitID, Vector2 Location)
{
#if __ANDROID__
// The actual ad
var bannerAd = new AdView(Game.Activity)
{
AdUnitId = adUnitID,
AdSize = AdSize.Banner,
};
bannerAd.LoadAd(new AdRequest.Builder()
#if DEBUG
.AddTestDevice("DEADBEEF9A2078B6AC72133BB7E6E177") // Prevents generating real impressions while testing
#endif
.Build());
if (Location != Vector2.Zero)
{
AdContainer.SetX(Location.X);
AdContainer.SetY(Location.Y);
}
// Give the game methods to show/hide the ad.
AdContainer.AddView(bannerAd);
#elif __IOS__
this.Location = new CGPoint(Location.X, Location.Y);
// Setup your BannerView, review AdSizeCons class for more Ad sizes.
AdViewBanner = new BannerView(size: AdSizeCons.Banner, origin: this.Location)
{
AdUnitID = adUnitID,
RootViewController = ViewController
};
// Wire AdReceived event to know when the Ad is ready to be displayed
AdViewBanner.AdReceived += (object sender, EventArgs e) =>
{
if (!isAddOnScreen)
ViewController.View.AddSubview(AdViewBanner);
isAddOnScreen = true;
AdsAreBeingReceived = true;
};
AdViewBanner.ReceiveAdFailed += (object sender, BannerViewErrorEventArgs e) =>
{
Console.WriteLine(e.Error.DebugDescription);
};
Request request = Request.GetDefaultRequest();
#if DEBUG
request.TestDevices = new[] { "GAD_SIMULATOR_ID", "kGADSimulatorID" };
#endif
AdViewBanner.LoadRequest(request);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment