Unity 4.2正式版開始添加了對Windows 8、Windows Phone 8等其他平台的支持,而且開發者可以免費使用Unity引擎來開發游戲了。而作為Windows Phone和Windows 8平台上最大的游戲社交網絡,OpenXLive也可以無縫支持Unity for WP8和Windows 8的開發。
本篇文章將介紹如何在Unity for WP8中調用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,創建一個新的工程:
我們為攝像機添加一個簡單的C#腳本,在Project窗口的Assets文件下右鍵,選擇Create->C# Script:
創建完成后雙擊改腳本文件,會自動打開MonoDevelop或者Visual Studio,具體的切換方式在Edit->Preferences…->External Tools中進行設置。
打開后可以看到默認的代碼:
將上述代碼全部刪除,添加一個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:
之后將此場景保存為DemoScene,到這里在Unity中的編輯就完成了。接下來將Unity工程導出為WP8的工程:
打開導出的WP8工程,在引用節點添加對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
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
技術支持郵箱:support@openxlive.com