@Paul_Crawley wrote:
For a VERY long time a bunch of people have been asking how to integrate Admob into there Android MonoGame Apps, After traveling around the Web and trying MANY examples, Most in Java and All NEVER a complete guild, I've completed the simplest and FULL example on how to do the simplest way possible using Visual Studio 2017 and the Latest Monogame 3.6 Dev. Here it is.
Firstly after creating your Android App Open "Manage NuGet Packages" (Right click project->Manage NuGet Packages)
Browse search for this "Xamarin.GooglePlayServices.Ads.Lite" and install itif its a new Project, create an Android manifest (Right Click project->Prooperties->Android Manifest->Create)
Insert the following via editing the Android Manifest.xml<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- Below MUST be put into the manifest to allow the ad to react to the user--> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
Heres and example of MY FULL Manifest
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AdController.AdController" android:versionCode="1" android:versionName="1.0" android:installLocation="auto"> <uses-sdk android:targetSdkVersion="22" android:minSdkVersion="22" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- Below MUST be put into the manifest to allow the ad to react to the user--> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> <application android:label="AdController"></application> </manifest>
Next Make a new Class Call it what ever you want, I called Mine AdController my NameSpace/Project was also called AdController so don't forget to change the namespace to yours
Here is the complete code for the Class
using Android.Gms.Ads; /// <summary> /// For Ads to work /// NuGet Xamarin.GooglePlayServices.Ads.Lite /// Only then can ads be added to the project /// /// AndroidManifest.xml File /// add these lines // <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> // <uses-permission android:name="android.permission.INTERNET" /> // <!-- Below MUST be put into the manifest to allow the ad to react to the user--> // <activity android:name="com.google.android.gms.ads.AdActivity" // android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> /// /// </summary> namespace AdController { public static class AdController { public static InterstitialAd interstitial=null; // APP ID This is a test use yours from Admob private static readonly string appID = "ca-app-pub-3940256099942544~3347511713"; // AD ID's This is a test use yours from Admob private static readonly string adID1 = "ca-app-pub-3940256099942544/1033173712"; private static readonly string adID2 = "ca-app-pub-YOUR AD ID GOES IN HERE"; // other ads etc public static bool adLoaded=false; /********************************************************************************************* * Function Name : InitAdRegularAd * Description : Show a regular full page ad * if you need to change the adID, just set interstitial.AdUnitId to point to another adIDx * ******************************************************************************************/ public static void InitRegularAd() { if(interstitial==null) { MobileAds.Initialize(Game1.Activity,appID); // initialize the ads interstitial = new InterstitialAd((Activity1)Game1.Activity); interstitial.AdUnitId = adID1; // adID string, can repoint this on the fly } Game1.Activity.RunOnUiThread(() => { if(interstitial.AdListener != null) // do we already have a listener? { interstitial.AdListener.Dispose(); // kill it if we do } interstitial.AdListener = null; var listening = new Listening(); listening.AdLoaded +=() => { if(interstitial.IsLoaded) { adLoaded = true; } }; interstitial.AdListener = listening; interstitial.LoadAd(new AdRequest.Builder().Build()); }); } /********************************************************************************************* * Function Name : ShowRegularAd * Description : display the ad * ******************************************************************************************/ public static void ShowRegulaAd() { if(adLoaded) { adLoaded = false; interstitial.Show(); } } } /********************************************************************************************* * Function Name : Listening * Description : Listening class for the add * ******************************************************************************************/ internal class Listening : AdListener { public delegate void AdLoadedEvent(); public delegate void AdClosedEvent(); public delegate void AdOpenedEvent(); // Declare the event. public event AdLoadedEvent AdLoaded; public event AdClosedEvent AdClosed; public event AdOpenedEvent AdOpened; public override void OnAdLoaded() { if(AdLoaded != null) { this.AdLoaded(); } base.OnAdLoaded(); } public override void OnAdClosed() { if(AdClosed != null) { this.AdClosed(); } // load the next ad ready to display AdController.interstitial.LoadAd(new AdRequest.Builder().Build()); base.OnAdClosed(); } public override void OnAdOpened() { if(AdOpened != null) { this.AdOpened(); } base.OnAdOpened(); } } }
And Here is how I called it as an example from MonoGame Game1.cs
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> private SpriteFont font; protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load<SpriteFont>("debugFont"); // GET AD'S READY TO BE DISPLAYED AdController.InitRegularAd(); // TODO: use this.Content to load your game content here } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// game-specific content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { Exit(); } // TODO: Add your update logic here base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> /// double oldCount = 0; protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); if(oldCount < gameTime.TotalGameTime.TotalMilliseconds) { oldCount = gameTime.TotalGameTime.TotalMilliseconds + 3000; if(AdController.adLoaded) { AdController.ShowRegulaAd(); } } else spriteBatch.DrawString(font,"Waiting on the Next AD!!!",new Vector2(380,300),Color.White); spriteBatch.End(); base.Draw(gameTime); }
That's it, Nothing else is required to display full page ads, NOT banners and NOT rewards ADS, i'm working on the Reward Type Ad's right now, once i've figured out the simplest way to do it i'll include it in this class and let you guys know. Obviously if anyone can add anything to this let me know, if you have any problems then let me know, i'm only to happy to help.
To use your own AD'S make sure you have an account from ADMOB.COM.
Just one other FYI
Hitting Close on the AD returns back to the App
Hitting the AD to activate it opens a browser, to return to the App NO NOT CLOSE THE BROWSER, AdMob opens the browser as a thread from the App, hit the Back (sideways triangle) to return to your AppEnjoy
Paul
Posts: 1
Participants: 1