本文說明如何將現有遊戲從遊戲第 1 版 SDK 遷移至遊戲第 2 版 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 專案
如要將外掛程式匯入 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」。
在「目標 API 級別」方塊中選取版本。
在「Scripting backend」(指令碼後端) 方塊中輸入
IL2CPP
。在「目標架構」方塊中選取值。
請記下套件名稱 package_name,您稍後會用到這項資訊。
Unity 專案中的「Player settings」。
更新自動登入程式碼
將 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 遊戲服務使用者介面中未顯示登出選項。
確認您能順利擷取玩家 ID,且伺服器端功能運作正常 (如適用)。
如果遊戲使用伺服器端驗證,請徹底測試
requestServerSideAccess
流程。確認伺服器收到驗證碼,並可交換存取權杖。測試網路錯誤、無效client ID
情境的成功和失敗情境。
如果遊戲使用下列任何功能,請測試這些功能,確保運作方式與遷移前相同:
- 排行榜:提交分數並查看排行榜。檢查玩家名稱和分數的排名和顯示方式是否正確。
- 成就:解鎖成就,並確認成就已正確記錄及顯示在 Play 遊戲使用者介面中。
- 已儲存的遊戲:如果遊戲使用已儲存的遊戲,請確保遊戲進度儲存和載入作業順利進行。因此請務必在多部裝置上測試,並在應用程式更新後進行測試。
遷移後工作
遷移至 Games V2 SDK 後,請完成下列步驟。