Last active
July 20, 2018 10:29
-
-
Save nanexcool/b874633811977f42ca7a to your computer and use it in GitHub Desktop.
Using AdMob on a MonoGame for Android game
This file contains 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 System; | |
using Android.App; | |
using Android.Content; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Android.OS; | |
using Android.Gms.Ads; | |
using Microsoft.Xna.Framework; | |
namespace MyGame | |
{ | |
[Activity (Label = "MyGame", | |
MainLauncher = true, | |
Icon = "@drawable/icon", | |
Theme = "@style/Theme.Splash", | |
AlwaysRetainTaskState = true, | |
LaunchMode = Android.Content.PM.LaunchMode.SingleTask, | |
ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, | |
ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | | |
Android.Content.PM.ConfigChanges.KeyboardHidden | | |
Android.Content.PM.ConfigChanges.Keyboard)] | |
public class Activity1 : AndroidGameActivity | |
{ | |
private const string AD_UNIT_ID = "ca-app-pub-NNNNNNNNNNNNNNNNNNNNNNNNNNN"; | |
private const string INTERSTITIAL_AD_UNIT_ID = "ca-app-pub-NNNNNNNNNNNNNNNNNNNNNNNNNN"; | |
private const string TEST_DEVICE_ID = "YOUR_TEST_DEVICE_ID"; | |
private AdView adView; | |
private InterstitialAd intAdView; | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
// Create our OpenGL view, and display it | |
var g = new Game1(); | |
createAds(g.Services.GetService<View>()); | |
g.ad = adView; | |
g.interstitial = intAdView; | |
g.Reload = ReloadInterstitial; | |
g.Run(); | |
} | |
// Wrapped everything in a function for less confusion. | |
// Thanks to Dylan Wilson at Craftwork Games for the | |
// simple layout | |
private void createAds(View window) | |
{ | |
var frameLayout = new FrameLayout(this); | |
var linearLayout = new LinearLayout(this); | |
frameLayout.AddView(window); | |
adView = new AdView(this); | |
adView.AdUnitId = AD_UNIT_ID; | |
adView.AdSize = AdSize.SmartBanner; | |
adView.SetBackgroundColor(Android.Graphics.Color.Black); | |
intAdView = new InterstitialAd(this); | |
intAdView.AdUnitId = INTERSTITIAL_AD_UNIT_ID; | |
linearLayout.AddView(adView); | |
frameLayout.AddView(linearLayout); | |
SetContentView(frameLayout); | |
try | |
{ | |
// Initiate a generic request. | |
var adRequest = new AdRequest.Builder() | |
.AddTestDevice(AdRequest.DeviceIdEmulator) | |
.AddTestDevice(TEST_DEVICE_ID) | |
.Build(); | |
// Load the adView with the ad request. | |
adView.LoadAd(adRequest); | |
ReloadInterstitial(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
public void ReloadInterstitial() | |
{ | |
try | |
{ | |
var request = new AdRequest.Builder() | |
.AddTestDevice(TEST_DEVICE_ID) | |
.Build(); | |
intAdView.LoadAd(request); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using Android.Gms.Ads; = ERROR What NuGet packages are needed ????
g.ad = adView;
All Give ERRORS AGAIN WHAT NUGET PACKAGES ARE REQUIRED!!!!!