Windows Phone 7 For XNA 广告盈利

  “Adcertising Services”广告服务控件是微软在Windows Phone平台上推出的赚钱利器,Adcertising Services可以轻松的帮助开发者在其开发的应用程序或者XNA游戏上显示广告。


使用步骤

  A)下载Microsoft Adcertising SDK for Windows Phone,并安装。安装完成后会你会得到两个框架的组件,一个是基于SL的,一个是基于XNA的。

  B) 登陆PubCenter,PubCenter主要的功能是创建并管理您的广告。在PubCenter中微软提供了贴心的广告解决方案帮你管理你的应用程序中广告的显示次数,盈利分析等功能。需要注意的是,登陆PubCenter时最好使用IE8.0或火狐5.0以上版本登陆,用其他浏览器登陆时会出现浏览器兼容问题。

  C) 创建并提交。


使用流程

  A)注册

      1)开打PubCenter后,使用你的开发者账号登陆。如下图:


                   

      2)如果你登陆成功的话,恭喜你,你已经离成功不远了 :)。登陆成功后你将完成注册Application ID、Ad units ID等工作。如下图:


       

         Reports:报表,这里主要表述了你的盈利情况,也可以查看广告不同时期的盈利,当然你还可以根据自己的需求来创建一个报表并保存。

         Setup:创建广告的入口,下面会详细列出步骤。

       Accounts:开发者账号个人信息。

      3)创建你的广告,点击Setup将会出现下图

                   

                   Applications:用来创建你的应用程序ID。

        Ad units:用来创建你广告单元的ID。

        Global ad exclusions:全球广告排除。添加你要阻止的URL。

        Channels:广告渠道。

        点击Applications->点击Register application,见下图:

      

                  

                  

        Name:因为是测试用,所有名字可以随意起:)

        Device type:选不了,默认!


                   点击Save进行保存来到下个页面。如下图:


                 

                   接下来该创建广告单元了,点击Setup下面的Ad units,在出现的页面中点击Create application ad unit,如下图:

                 

                   在出现的页面中填入Ad unit name,并选择广告单元的大小,这里提供了两种尺寸,见下图:

                

                   添加完Ad unit name和Size后我们还要选择一下广告的类别,在类别选中我们只能选三个。如下图:


                  

                   添加Global ad exclusions。如果你不想看到哪个网站的广告,你可以在下面的对话框中填入相应的Url。不填写也没问题。

                   点击SAVE进行保存后将出现一个关于Ad unit的详细列表,在这个列表中点击Ad unit Name进行修改,也可以重新创建一个Ad unit。如下图:

                   

                 

      4)说明

         在开发过程中,如果你使用模拟器来查看刚刚申请的广告控件的话,会没有任何广告显示出血来,因为微软怕造成非法广告点击,而且微软的广告控件可以识别程序是否运行在模拟器上,所以在模拟器上广告控件是不会显示的,不过贴心的微软提供了三组测试数据,可以使用这些数据测试广告控件在Windows Phone 7中的实际效果。

         以下是三组测试数据的测试值,如下图:

                 

                 


  B)在XNA中使用Adcertising

      首先需要明确的一点是在Windows Phone for XNA中使用Adcertising与在Windows Phone for Silverlight中使用Adcertising是不同的,在SL中可以直接使用Adcertising控件进行控制,而在XNA中需要添加相关代码来实现,以下是微软提供的实例代码当然你也可以去下载加深理解!在自己添加的工作中,别忘记引用Microsoft.Advertising.Mobile.Xna.dllMicrosoft.Advertising.Mobile.Xna命名空间!

//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the Microsoft Public License.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Advertising.Mobile.Xna;
using System.Diagnostics;
using System.Device.Location;

namespace XnaGameWithAd
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
     
private static readonly string ApplicationId = "test_client"; // 如果在实际使用中,需要换成您之前申请的ApplicationId
        private static readonly string AdUnitId = "TextAd"; //other test values: Image480_80, Image300_50, TextAd,在实际使用中换成您之前申请的AdUnitId

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
DrawableAd bannerAd; // bannerAd代表一个广告单元

// We will use this to find the device location for better ad targeting.
private GeoCoordinateWatcher gcw = null;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// Initialize the AdGameComponent with your ApplicationId and add it to the game.
AdGameComponent.Initialize(this, ApplicationId);
Components.Add(AdGameComponent.Current);

// Now create an actual ad for display.
CreateAd();

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all 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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.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>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

base.Draw(gameTime);
}

/// <summary>
/// Create a DrawableAd with desired properties.
/// </summary>
private void CreateAd()
{
// Create a banner ad for the game.
int width = 480;
int height = 80;
int x = (GraphicsDevice.Viewport.Bounds.Width - width) / 2; // centered on the display
int y = 5;

bannerAd = AdGameComponent.Current.CreateAd(AdUnitId, new Rectangle(x, y, width, height), true);

// Add handlers for events (optional).
bannerAd.ErrorOccurred += new EventHandler<Microsoft.Advertising.AdErrorEventArgs>(bannerAd_ErrorOccurred);
bannerAd.AdRefreshed += new EventHandler(bannerAd_AdRefreshed);

// Set some visual properties (optional).
//bannerAd.BorderEnabled = true; // default is true
//bannerAd.BorderColor = Color.White; // default is White
//bannerAd.DropShadowEnabled = true; // default is true

// Provide the location to the ad for better targeting (optional).
// This is done by starting a GeoCoordinateWatcher and waiting for the location to be available.
// The callback will set the location into the ad.
// Note: The location may not be available in time for the first ad request.
AdGameComponent.Current.Enabled = false;
this.gcw = new GeoCoordinateWatcher();
this.gcw.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(gcw_PositionChanged);
this.gcw.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(gcw_StatusChanged);
this.gcw.Start();
}

/// <summary>
/// This is called whenever a new ad is received by the ad client.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bannerAd_AdRefreshed(object sender, EventArgs e)
{
Debug.WriteLine("Ad received successfully");
}

/// <summary>
/// This is called when an error occurs during the retrieval of an ad.
/// </summary>
/// <param name="sender"></param>
/// <param name="e">Contains the Error that occurred.</param>
private void bannerAd_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
{
Debug.WriteLine("Ad error: " + e.Error.Message);
}

private void gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// Stop the GeoCoordinateWatcher now that we have the device location.
this.gcw.Stop();

bannerAd.LocationLatitude = e.Position.Location.Latitude;
bannerAd.LocationLongitude = e.Position.Location.Longitude;

AdGameComponent.Current.Enabled = true;

Debug.WriteLine("Device lat/long: " + e.Position.Location.Latitude + ", " + e.Position.Location.Longitude);
}

private void gcw_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Disabled || e.Status == GeoPositionStatus.NoData)
{
// in the case that location services are not enabled or there is no data
// enable ads anyway
AdGameComponent.Current.Enabled = true;
Debug.WriteLine("GeoCoordinateWatcher Status :" + e.Status);
}
}

/// <summary>
/// Clean up the GeoCoordinateWatcher
/// </summary>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing)
{
if (this.gcw != null)
{
this.gcw.Dispose();
this.gcw = null;
}
}
}
}
}


      让我们看一下运行后的效果,如下图:

                 

        我顺手展示下自己开发的小游戏中加入的Adcertising,思路很简单就是在你游戏继承了Game的类中CreateAd()下,代码基本跟示例代码一致,但每个游戏的设计会有所不同,根据个人的设计自行调整下就可以了,如下图:

                 

                 

  

    OK,基本的流程大致就是这样,赶快行动吧!:)

版权所有欢迎转载





原文地址:https://www.cnblogs.com/youyang/p/2274793.html