天天看點

WP8:在Unity中使用OpenXLive

Unity 4.2正式版開始添加了對Windows 8、Windows Phone 8等其他平台的支援,而且開發者可以免費使用Unity引擎來開發遊戲了。而作為Windows Phone和Windows 8平台上最大的遊戲社交網絡,OpenXLive也可以無縫支援Unity for WP8和Windows 8的開發。

本篇文章将介紹如何在Unity for WP8中調用OpenXLive的各種服務。

WP8:在Unity中使用OpenXLive

安裝Unity 4.2正式版

Unity 4.2正式版下載下傳位址為:http://unity3d.com/unity/download/

安裝OpenXLive SDK

OpenXLive SDK最新版本的下載下傳位址為:http://developer.openxlive.net

在Unity工程中使用OpenXLive

打開Unity 4.2,建立一個新的工程:

WP8:在Unity中使用OpenXLive

我們為錄影機添加一個簡單的C#腳本,在Project視窗的Assets檔案下右鍵,選擇Create->C# Script:

WP8:在Unity中使用OpenXLive

建立完成後輕按兩下改腳本檔案,會自動打開MonoDevelop或者Visual Studio,具體的切換方式在Edit->Preferences…->External Tools中進行設定。

打開後可以看到預設的代碼:

WP8:在Unity中使用OpenXLive

将上述代碼全部删除,添加一個OnGUI方法,在其中繪制幾個按鈕:

1 void OnGUI()
 2 {
 3     if (GUILayout.Button("Game Center", GUILayout.Width(300),     GUILayout.Height(40)))
 4     {
 5     }
 6 
 7     if (GUILayout.Button("Submit Score", GUILayout.Width(300), GUILayout.Height(40)))
 8     {
 9     }
10 }      

在Unity的開發文檔中,可以參考這篇文章《Interaction between Unity and Windows Phone step by step guide》 :

http://docs.unity3d.com/Documentation/Manual/wp8-unity-interaction.html

介紹了如何在Unity和Windows Phone之間進行資料互動,我們同樣在C#腳本檔案中,添加一些傳回事件,使得這些事件在Windows Phone中被觸發,就可以在其中進行任何OpenXLive操作,包括顯示遊戲中心、送出分數、擷取成就、社互動動等等。

首先在C#腳本頂部添加對System的引用:

1 using System;      

然後添加以下事件:

1 public event EventHandler GameCenterButtonPressed;
2 public event EventHandler SubmitScoreButtonPressed;      

在按鈕被按下時分别傳回這些事件:

1 void OnGUI()
 2 {
 3     if (GUILayout.Button("Game Center", GUILayout.Width(300), GUILayout.Height(40)))
 4     {
 5         if (GameCenterButtonPressed != null)
 6         {
 7             GameCenterButtonPressed(this, EventArgs.Empty);
 8         }
 9     }
10 
11     if (GUILayout.Button("Submit Score", GUILayout.Width(300), GUILayout.Height(40)))
12     {
13         if (SubmitScoreButtonPressed != null)
14         {
15             SubmitScoreButtonPressed(this, EventArgs.Empty);
16         }
17     }
18 }
19         

接下來傳回Unity,把這個C#腳本應用到錄影機上,直接拖拽該檔案到錄影機上即可;或者點選錄影機,在Inspector視窗中,點選Add Component按鈕,添加一個MyScript:

WP8:在Unity中使用OpenXLive
WP8:在Unity中使用OpenXLive

之後将此場景儲存為DemoScene,到這裡在Unity中的編輯就完成了。接下來将Unity工程導出為WP8的工程:

WP8:在Unity中使用OpenXLive

打開導出的WP8工程,在引用節點添加對OpenXLive的引用:

WP8:在Unity中使用OpenXLive

打開App.xaml.cs,在構造函數中,啟動OpenXLive會話并對UI進行初始化:

1 GameSession session = XLiveGameManager.CreateSession(APISecretKey); 
 2 XLiveUIManager.Initialize(this, session);
 3 session.CreateSessionCompleted += session_CreateSessionCompleted;
 4 session.Open();
 5 
 6 void session_CreateSessionCompleted(object sender, AsyncEventArgs e)
 7 {
 8     if (e.Result.ReturnValue)
 9     {
10     }
11 }      

接下來打開MainPage.xaml.cs,在Unity_Loaded方法中,取出Unity的C#腳本對象:

1 private void Unity_Loaded()
2 {
3     MyScript script = (MyScript)UnityEngine.Object.FindObjectOfType(typeof(MyScript));
4     script.GameCenterButtonPressed += script_GameCenterButtonPressed;
5     script.SubmitScoreButtonPressed += script_SubmitScoreButtonPressed;
6 }      

注意注冊事件必須且隻能在UI線程中進行操作,如:

1 void script_GameCenterButtonPressed(object sender, EventArgs e)
 2 {
 3     this.Dispatcher.BeginInvoke(delegate
 4     {
 5         XLiveUIManager.ShowGameCenter();
 6     });
 7 }
 8 
 9 void script_SubmitScoreButtonPressed(object sender, EventArgs e)
10 {
11     this.Dispatcher.BeginInvoke(delegate
12     {
13         var lbp = XLiveGameManager.CurrentSession.LeaderboardProfiles[0];
14         Leaderboard lb = new Leaderboard(XLiveGameManager.CurrentSession, lbp);
15         lb.SubmitScoreCompleted += lb_SubmitScoreCompleted;
16         lb.SubmitScore(10);
17     });
18 }      

這樣就可以在Unity的遊戲邏輯中調用OpenXLive的相關功能了,特别是送出分數、擷取成就等功能。更多OpenXLive相關功能,請檢視OpenXLive SDK幫助文檔,或通路開發者網站擷取。

參考資料

OpenXLive Website

http://www.openxlive.com/

OpenXLive Developer Website

http://developer.openxlive.com/

Getting Started with Open XLive

http://wiki.openxlive.com/Getting-Started-with-Open-XLive.ashx

更多問題,可以通路我們的論壇

英文版論壇位址:http://bbs.openxlive.com

中文版論壇位址:http://bbs.openxlive.net/  

QQ群:149993869

技術支援郵箱:[email protected] 

新浪微網誌:http://weibo.com/openxlive