為了彌補前失,所以小弟今天特意下載了一些支持XNA的廣告SDK,并且改進了XNA與LGame的交互機制,做成了程序示例放入C#版中(只顯示廣告條和FPS的空項目,方便套用),下面是微軟提供的Advertising廣告,以及第三方XNA廣告組件wp7adrotator和LGame的混用示例。
Advertising
using Loon;
using Loon.Utils.Debug;
using Loon.Core.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Advertising.Mobile.Xna;
using System.Diagnostics;
using System.Device.Location;
using System;
namespace LGameAd
{
/// <summary>
/// 構建XNA監聽,用以展示廣告
/// </summary>
public class ADListener : XNAListener
{
//Advertising測試用標記(微軟硬性規定,只有傳這個才能啟動Advertising測試)
private static readonly string ApplicationId = "test_client";
//廣告單元ID(測試時只支持4種顯示模式,就是Image480_80、Image480_80、Image300_50、TextAd,正式ID后才能自定義。)
private static readonly string AdUnitId = "Image480_80";
private DrawableAd bannerAd;
//廣告驅動定位器(用來通過GPS/AGPS找到你手機的物理位置)
private GeoCoordinateWatcher gcw = null;
/// <summary>
/// LGame監聽接口,用來監聽標準XNA中Game類的構建
/// </summary>
/// <param name="game"></param>
public void Create(Game game)
{
}
/// <summary>
/// LGame監聽接口,用來監聽標準XNA中Initialize的啟動
/// </summary>
public void Initialize(Game game)
{
//初始化AdGameComponent組件,并將其添加到游戲中
AdGameComponent.Initialize(game, ApplicationId);
game.Components.Add(AdGameComponent.Current);
//創建一個新的廣告
CreateAd(game);
}
/// <summary>
/// LGame監聽接口,用來監聽標準XNA中LoadContent的啟動
/// </summary>
public void LoadContent(Game game)
{
}
/// <summary>
/// LGame監聽接口,用來監聽標準XNA中UnloadContent的啟動
/// </summary>
public void UnloadContent(Game game)
{
}
/// <summary>
/// LGame監聽接口,用來監聽標準XNA中Updatet的調用(每幀循環時都會調用)
/// </summary>
public void Update(Game game, GameTime gameTime)
{
}
/// <summary>
/// LGame監聽接口,用來監聽標準XNA中Draw的調用(每幀循環時都會調用)
/// </summary>
public void Draw(Game game, GameTime gameTime)
{
}
/// <summary>
/// 創建廣告
/// </summary>
private void CreateAd(Game game)
{
// 創建指定大小的廣告組件
int width = 480;
int height = 80;
// 定位到屏幕中央上方
int x = (game.GraphicsDevice.Viewport.Bounds.Width - width) / 2;
int y = 5;
bannerAd = AdGameComponent.Current.CreateAd(AdUnitId, new Rectangle(x, y, width, height), true);
// 添加廣告事件監聽
bannerAd.ErrorOccurred += new EventHandler<Microsoft.Advertising.AdErrorEventArgs>(bannerAd_ErrorOccurred);
bannerAd.AdRefreshed += new EventHandler(bannerAd_AdRefreshed);
// 并不是立即激活廣告(在GPS定位成功后才激活)
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();
}
private void bannerAd_AdRefreshed(object sender, EventArgs e)
{
Log.DebugWrite("Ad received successfully");
}
private void bannerAd_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
{
Log.DebugWrite("Ad error: " + e.Error.Message);
}
private void gcw_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
this.gcw.Stop();
bannerAd.LocationLatitude = e.Position.Location.Latitude;
bannerAd.LocationLongitude = e.Position.Location.Longitude;
AdGameComponent.Current.Enabled = true;
Log.DebugWrite("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)
{
AdGameComponent.Current.Enabled = true;
Log.DebugWrite("GeoCoordinateWatcher Status :" + e.Status);
}
}
/// <summary>
/// LGame監聽接口,用來監聽標準XNA中Dispose的調用(游戲結束時才會調用到)
/// </summary>
public void Dispose(Game game, bool disposing)
{
if (disposing)
{
if (this.gcw != null)
{
this.gcw.Dispose();
this.gcw = null;
}
}
}
}
public class Game1 : LFXPlus
{
public override void OnMain()
{
//加載LGame默認資源(不進行此操作,LGame內置的模擬按鈕之類功能無法使用)
XNAConfig.Load("assets/loon.def");
//加載字體文件(此處是預編譯好的xnb文件,也可以加載Content下的)
XNAFont = new LFont("assets", "black", 0, 20);
//注冊AD監聽(標準XNA事件監聽)
SetXNAListener(new ADListener());
//設定啟動參數
LSetting setting = new LSetting();
setting.fps = 60;
setting.width = 480;
setting.height = 320;
setting.showFPS = true;
setting.landscape = true;
//注冊初始Screen
Register(setting, typeof(ScreenTest));
}
public override void OnGameResumed()
{
}
public override void OnGamePaused()
{
}
}
}
然后,是利用wp7adrotator這個開源的第三方廣告組件,加載AdDuplex廣告(此物也支持Admob)。
using System;
using System.Windows;
using AdRotatorXNA;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Loon;
using Loon.Core.Graphics;
namespace AdRotatorExampleXNA
{
/// <summary>
/// 創建XNA監聽器(稍微解釋一下LGame中所謂的XNA監聽器。本質上說,LGame-XNA版其實就是一個XNA的封裝馬甲。所以此監聽器的實際作用,就是
/// 在LGame處理完畢后,把XNA應有的操作權限在監聽中顯示出來罷了~)
/// </summary>
public class ADListener : XNAListener
{
public void Create(Game gamne)
{
}
public void Initialize(Game game)
{
// 初始化廣告組件
AdRotatorXNAComponent.Initialize(game);
//硬編碼的話就填下面這些
//AdRotatorXNAComponent.Current.PubCenterAppId = "test_client";
//AdRotatorXNAComponent.Current.PubCenterAdUnitId = "Image480_80";
//AdRotatorXNAComponent.Current.AdDuplexAppId = "0";
//AdRotatorXNAComponent.Current.InneractiveAppId = "DavideCleopadre_ClockAlarmNightLight_WP7";
//AdRotatorXNAComponent.Current.MobFoxAppId = "474b65a3575dcbc261090efb2b996301";
//AdRotatorXNAComponent.Current.MobFoxIsTest = true;
//讀取配置文件的話就填下面這些(本例為讀取AdDuplex的測試廣告,AdRotator也支持Admob廣告)
//定位廣告位置
AdRotatorXNAComponent.Current.AdPosition = new Vector2(0,720);
//設定默認的廣告圖片
AdRotatorXNAComponent.Current.DefaultHouseAdImage = game.Content.Load<Texture2D>(@"Content/AdRotatorDefaultAdImage");
//當點擊默認廣告時,指向此操作。
AdRotatorXNAComponent.Current.DefaultHouseAdClick += new AdRotatorXNAComponent.DefaultHouseAdClickEventHandler(Current_DefaultHouseAdClick);
//用以選擇廣告幻燈效果的彈出方向
AdRotatorXNAComponent.Current.SlidingAdDirection = SlideDirection.None;
//選擇本地的廣告配置文件地址(針對不同的廣告商,此處配置效果不同,以具體廣告商提供的配置方式為準)
AdRotatorXNAComponent.Current.DefaultSettingsFileUri = "defaultAdSettings.xml";
//設定遠程配置文件(可選項,非必填)
AdRotatorXNAComponent.Current.SettingsUrl = "http://xna-uk.net/adrotator/XNAdefaultAdSettingsV2.xml";
//添加廣告組件到XNA畫面當中
game.Components.Add(AdRotatorXNAComponent.Current);
}
void Current_DefaultHouseAdClick()
{
try
{
MessageBox.Show("非常感謝您點了小弟的廣告^_^");
}
catch { }
}
public void LoadContent(Game game)
{
}
public void UnloadContent(Game game)
{
}
public void Update(Game game,GameTime gameTime)
{
}
public void Draw(Game game, GameTime gameTime)
{
}
public void Dispose(Game game, bool close)
{
}
}
public class Game1 : LFXPlus
{
public override void OnMain()
{
//加載LGame默認資源(不進行此操作,LGame內置的模擬按鈕之類功能無法使用)
XNAConfig.Load("content/loon.def");
//加載字體文件(此處是預編譯好的xnb文件 PS:當自定義資源文件夾命名為Content時,
//打包后會自動和標準的Content文件夾合并,這里做個演示。另,Windows系統不區分文件
//名大小寫)
XNAFont = new LFont("content", "black", 0, 20);
//注冊AD監聽(標準XNA事件監聽)
SetXNAListener(new ADListener());
//設定啟動參數
LSetting setting = new LSetting();
setting.fps = 60;
setting.width = 480;
setting.height = 320;
setting.showFPS = true;
setting.landscape = false;
//注冊初始Screen
Register(setting, typeof(ScreenTest));
}
public override void OnGameResumed()
{
}
public override void OnGamePaused()
{
}
}
}
雖然目前很多Ad廠商只提供了Silverlight的廣告SDK支持,但若考慮到國外第三方開發者的貢獻,其實幾乎所有常見WP7 Ad SDK(國外),都有辦法通過XNA進行部署,并不一定非要Silverlight支持。
——但是,國內的廣告商們似乎就沒這么好運了。
今天(周二)試驗了一下Silverlight+XNA混用,如預想中很好實現,目前已經添加了一個名為LSilverlight-0.3.3.dll的新編譯文件與相關源碼到WP7部分,等周五上傳后就能支持Silverlight了(不立即傳,是因為小弟目前版本多開(Java、C#、C/C++、HTML5(JS)),只能攢夠修正一起來-_-|||),具體寫法如下所示:
namespace LGameTest
{
using System.Windows;
using System.Windows.Navigation;
using Loon;
using Loon.Core.Graphics;
using Loon.Core.Input;
using Loon.Core.Timer;
using Loon.Core.Graphics.OpenGL;
using Microsoft.Phone.Controls;
public class ScreenTest : Screen
{
public override LTransition OnTransition()
{
return LTransition.NewEmpty();
}
public override void OnLoad()
{
}
public override void Alter(LTimerContext c)
{
}
public override void Draw(GLEx g)
{
}
public override void TouchDown(LTouch touch)
{
}
public override void TouchDrag(LTouch e)
{
}
public override void TouchMove(LTouch e)
{
}
public override void TouchUp(LTouch touch)
{
}
}
//微軟硬性規定此處的PhoneApplicationPage必須是原始類,所以LGame在使用Silverlight時就只能采取如下加載方式了……
public partial class GamePage : PhoneApplicationPage
{
LSilverlightPlus plus;
public GamePage()
{
InitializeComponent();
//加載Silverlight數據到LGame
plus = LSilverlightPlus.Load(this, (Application.Current as App).Content, OnMain);
}
/// <summary>
/// 初始化事件
/// </summary>
/// <param name="plus"></param>
public void OnMain(LSilverlightPlus plus)
{
//加載LGame默認資源(不進行此操作,LGame內置的模擬按鈕之類功能無法使用)
XNAConfig.Load("assets/loon.def");
//加載字體文件(此處是預編譯好的xnb文件,也可以加載Content下的)
plus.XNAFont = new LFont("assets", "black", 0, 20);
//設定啟動參數
LSetting setting = new LSetting();
setting.fps = 60;
setting.width = 480;
setting.height = 320;
setting.showFPS = true;
setting.landscape = false;
//注冊初始Screen
plus.Register(setting, typeof(ScreenTest));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (plus != null)
{
plus.OnNavigatedTo(e);
base.OnNavigatedTo(e);
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
if (plus != null)
{
plus.OnNavigatedFrom(e);
base.OnNavigatedFrom(e);
}
}
}
}
為了能兼顧國內的第三方廣告商,所以小弟周五將添加Silverlight+XNA的混用類庫(其實代碼修改量很小,主要集中于渲染和輸入輸出接口部分,但小弟不做也不會憑空出現吧……望天……),到時會將上述修正一并上傳到SVN。
——————————————
下面順手發個微軟與蘋果平板發布時的對比視頻,話說55秒開始神作了。
蘋果iPad和微軟Surface發布會對比視頻
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

