12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using Android.App;
- using Android.Content;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.OS;
- using Microsoft.Xna.Framework;
- using Android.Gms.Ads;
- using Android.Content.PM;
- namespace MonoGame.Samples.AdMob
- {
- [Activity (
- Label = "AdMob",
- MainLauncher = true,
- Icon = "@drawable/icon",
- Theme = "@style/Theme.Splash",
- ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.Keyboard|ConfigChanges.KeyboardHidden)]
- public class Activity1 : AndroidGameActivity
- {
- private Game1 _game;
- private View _view;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Initialize Google Mobile Ads SDK
- MobileAds.Initialize(this);
- // Create the MonoGame view
- _game = new Game1();
- _view = _game.Services.GetService(typeof(View)) as View;
- // Create a layout to hold both the game and the ad
- var layout = new LinearLayout(this)
- {
- Orientation = Orientation.Vertical
- };
- // Add the MonoGame view
- layout.AddView(_view, new LinearLayout.LayoutParams(
- ViewGroup.LayoutParams.MatchParent,
- 0, 1f)); // Weight 1 to fill remaining space
- // Create and add the AdMob banner
- var adView = new AdView(this)
- {
- AdSize = AdSize.Banner,
- AdUnitId = "ca-app-pub-3940256099942544/6300978111" // Test Ad Unit ID
- };
- var adParams = new LinearLayout.LayoutParams(
- ViewGroup.LayoutParams.MatchParent,
- ViewGroup.LayoutParams.WrapContent);
- layout.AddView(adView, adParams);
- // Load an ad
- var adRequest = new AdRequest.Builder().Build();
- adView.LoadAd(adRequest);
- // Set the layout as the content view
- SetContentView(layout);
- _game.Run();
- }
- }
- }
|