如何在最新的MonoGame Android(XNA)中集成AdMob广告?

Fel*_*ipe 4 android admob monogame

我花了最近几天研究MonoGame Android的AdMob集成,到目前为止还没能成功地为我刚刚制作的游戏添加横幅.到目前为止,我发现的所有答案都非常过时,我找到的所有示例都没有在最新的Android API中使用.

我在Visual Studio中使用MonoGame 3.2的开发版#983.

我试过了:

以及在互联网上发现的其他方法.在向项目添加JAVA源和JAR文件时,我一直非常谨慎地选择正确的构建选项,但我从未找到任何AdView类,而且那里的"Google Mobile Ads SDK v6.4.1"JAR不再存在谷歌支持,如下所述:https://developers.google.com/mobile-ads-sdk/

如果有人有任何新的和最新的方法将AdMob广告整合到MonoGame Android项目中,我认为答案需要刷新(我将非常感激)=)

Fel*_*ipe 9

经过很长一段时间寻找答案和测试多个建议,我终于找到了解决方案.

Xamarin有一个Google Play服务组件,可以从组件商店下载.我最初尝试过这个,但在VS2012中我的解决方案文件遇到了问题.这就是我手动添加组件所做的:

  • https://components.xamarin.com/view/googleplayservices/登录并下载该组件

  • 在Android解决方案中创建一个名为"lib"的文件夹,并将压缩组件文件夹中的dll文件解压缩到其中

  • 添加dll文件作为项目的引用.您现在应该添加以下四个引用:

    1. GooglePlayServicesLib
    2. Xamarin.Android.Support.v4
    3. Xamarin.Android.Support.v7.AppCompat
    4. Xamarin.Android.Support.v7.MediaRouter
  • 导航到项目属性,单击Application选项卡(第一个),然后在"Java Max Heap Size"下输入1G(否则编译时会出现Java Heap内存不足错误)

  • 确保您的应用程序设置为使用API​​ 14或更高版本(Android 4.0).这可以通过项目属性页面来完成

  • 在AndroidManifest.xml文件中,确保您拥有以下内容:

    <activity 
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    
    Run Code Online (Sandbox Code Playgroud)
  • 获取您的AdMob单元ID

  • 将Activity.cs文件更改为以下内容:

    using Android.Gms.Ads; // Add this include
    
    // Easy constants
    private const string AD_UNIT_ID = "YOUR_AD_ID";
    private const string TEST_DEVICE_ID = "YOUR_DEVICE_ID";
    private AdView adView;
    
    // Change OnCreate and make sure you're not trying to set SetContentView()
    // more than once
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Puzzle.ARunningMan.Activity = this;
        var g = new Puzzle.ARunningMan();
    
        createAds(g.Window);
    
        g.Run();
    }
    
    // Wrapped everything in a function for less confusion.
    // Thanks to Dylan Wilson at Craftwork Games for the
    // simple layout
    private void createAds(AndroidGameWindow window)
    {
        var frameLayout = new FrameLayout(this);
        var linearLayout = new LinearLayout(this);
    
        linearLayout.Orientation = Orientation.Horizontal;
        linearLayout.SetGravity(Android.Views.GravityFlags.Right | Android.Views.GravityFlags.Bottom);
    
        frameLayout.AddView(window);
    
        adView = new AdView(this);
        adView.AdUnitId = AD_UNIT_ID;
        adView.AdSize = AdSize.Banner;
    
        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);
        }
        catch (Exception ex)
        {
            // your error logging goes here
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 编译(首先需要一段时间),运行它,在LogCat窗口中查找标记为包含您的设备ID的"广告"的消息.更多信息:http://webtutsdepot.com/2011/12/02/android-sdk-tutorial-get-admob-test-device-id/

  • 设置设备ID字符串,重新编译,运行它

最后,如果您等待大约30-60秒,您将看到测试添加显示为横幅.祝大家好运,我希望这会有所帮助,因为它现在是当前的信息