本文說明如何將現有遊戲從 games v1 SDK 遷移至 games v2 SDK。Unity 適用的 Play 遊戲外掛程式 (10 以下版本) 會使用遊戲服務第 1 版 SDK。
事前準備
- 請確認您已設定 Play 管理中心並安裝 Unity Hub。
下載 Unity 專用 Google Play 遊戲外掛程式
如要享有 Play 遊戲服務的最新功能,請下載並安裝最新外掛程式版本。請從 GitHub 存放區下載。
移除舊外掛程式
在 Unity Hub 中移除下列資料夾或檔案。
Assets/GooglePlayGames Assets/GeneratedLocalRepo/GooglePlayGames Assets/Plugins/Android/GooglePlayGamesManifest.androidlib Assets/Plugins/Android
![移除 Unity 專案中醒目顯示的資料夾。](https://developer.android.com/static/images/games/pgs/unityfolders.png?hl=zh-tw)
將新外掛程式匯入 Unity 專案
如要將外掛程式匯入 Unity 專案,請按照下列步驟操作:
- 開啟遊戲專案。
- 在 Unity Hub 中,依序點選「Assets」>「Import Package」>「Custom Package」,即可將下載的
unitypackage
檔案匯入專案的資產。 確認目前的建構平台已設為「Android」。
依序點選主選單的「File」>「Build Settings」。
選取「Android」,然後點選「Switch Platform」。
依序前往「Window」>「Google Play Games」,應該會顯示新的選單項目。如果沒有顯示,請依序點選「Assets」>「Refresh」重新整理資產,再嘗試重新設定建構平台。
在 Unity Hub 中,依序點選「File」>「Build Settings」>「Player Settings」>「Other Settings」。
在「Target API level」方塊中選取版本。
在「Scripting backend」方塊中輸入
IL2CPP
。在「Target architectures」方塊中,選取一個值。
請記下套件名稱 package_name。您稍後可以使用這項資訊。
Unity 專案中的玩家設定。
更新自動登入程式碼
將 PlayGamesClientConfiguration
初始化類別替換為 PlayGamesPlatform.Instance.Authenticate()
類別。不需要初始化和啟用 PlayGamesPlatform
。呼叫 PlayGamesPlatform.Instance.Authenticate()
會擷取自動登入的結果。
C#
在 Unity Hub 中,找出含有 PlayGamesClientConfiguration
類別的檔案。
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
public void Start() {
PlayGamesClientConfiguration config =
new PlayGamesClientConfiguration.Builder()
// Enables saving game progress
.EnableSavedGames()
// Requests the email address of the player be available
// will bring up a prompt for consent
.RequestEmail()
// Requests a server auth code be generated so it can be passed to an
// associated backend server application and exchanged for an OAuth token
.RequestServerAuthCode(false)
// Requests an ID token be generated. This OAuth token can be used to
// identify the player to other services such as Firebase.
.RequestIdToken()
.Build();
PlayGamesPlatform.InitializeInstance(config);
// recommended for debugging:
PlayGamesPlatform.DebugLogEnabled = true;
// Activate the Google Play Games platform
PlayGamesPlatform.Activate();
}
並更新為以下程式碼:
using GooglePlayGames;
public void Start() {
PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
}
internal void ProcessAuthentication(SignInStatus status) {
if (status == SignInStatus.Success) {
// Continue with Play Games Services
} else {
// Disable your integration with Play Games Services or show a login
// button to ask users to sign-in. Clicking it should call
// PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication).
}
}
選擇社群平台
如要選擇社群平台,請參閱選擇社群平台。
擷取伺服器驗證碼
如要取得伺服器端存取碼,請參閱「擷取伺服器驗證碼」。
移除登出程式碼
移除登出程式碼。Play 遊戲服務不再需要遊戲中的登出按鈕。
移除下列範例中顯示的程式碼:
C#
// sign out
PlayGamesPlatform.Instance.SignOut();
測試遊戲
請測試遊戲,確保遊戲能正常運作。您執行的測試取決於遊戲的功能。
以下列出常見的測試項目。
登入成功。
自動登入功能運作正常。使用者在啟動遊戲時應已登入 Play 遊戲服務。
畫面上會顯示歡迎彈出式視窗。
歡迎彈出式視窗範例 (按一下可放大)。 系統會顯示成功的記錄訊息。在終端機中執行下列指令:
adb logcat | grep com.google.android.
以下範例顯示成功的記錄訊息:
[
$PlaylogGamesSignInAction$SignInPerformerSource@e1cdecc number=1 name=GAMES_SERVICE_BROKER>], returning true for shouldShowWelcomePopup. [CONTEXT service_id=1 ]
確保 UI 元件一致性。
在 Play 遊戲服務使用者介面 (UI) 中,彈出式視窗、排行榜和成就會在各種螢幕大小和方向下正確且一致地顯示。
Play 遊戲服務 UI 中沒有登出選項。
確認您能成功擷取玩家 ID,並且伺服器端功能 (如適用) 能正常運作。
如果遊戲使用伺服器端驗證,請徹底測試
requestServerSideAccess
流程。請確認伺服器收到驗證碼,並能用來交換存取權杖。測試成功和失敗情境,檢查網路錯誤、無效的client ID
情境。
如果您的遊戲使用下列任何功能,請進行測試,確保這些功能的運作方式與遷移前相同:
- 排行榜:提交分數及查看排行榜。檢查排名是否正確,並確認玩家名稱和分數是否正確顯示。
- 成就:解鎖成就,並確認成就是否正確記錄並顯示在 Play 遊戲 UI 中。
- 儲存遊戲:如果遊戲使用儲存遊戲,請確保儲存及載入遊戲進度能夠正常運作。這對於在多部裝置上進行測試,以及在應用程式更新後測試,特別重要。
遷移後工作
遷移至 games v2 SDK 後,請完成下列步驟。