在繼續使用自己的帳戶系統時,順暢地讓使用者登入遊戲。您可以使用 Play 遊戲服務 Recall API,將遊戲內帳戶連結至 Google Play 遊戲服務帳戶。這樣一來,使用者在不同裝置上玩遊戲 (或重新安裝遊戲後在同一部裝置上玩),您就能查詢連結的遊戲內帳戶,簡化登入流程。
如果您已整合 Android Recall API,應該會對這些 Recall API 感到熟悉。與 Play 遊戲服務 Recall 整合的伺服器端,可供電腦版遊戲重複使用,因為 Android 和電腦版都相同。
必要條件
完成 SDK 設定。
在 Play 管理中心完成 Google Play 遊戲服務設定。
步驟 1:在資訊清單中新增 Play 遊戲服務專案 ID
在 Play 管理中心完成 Play 遊戲服務設定後,遊戲現在會與 Play 遊戲服務的專案 ID 建立關聯。使用這個專案 ID (位於 Play 管理中心的 Play 遊戲服務「設定」頁面),更新遊戲的 manifest.xml。
manifest.xml 內容範例:
<?xml version="1.0" encoding="utf-8"?>
<Manifest version="1">
<Application>
<PackageName>com.example.package</PackageName>
<PlayGamesServices>
<ProjectId>123456789</ProjectId>
</PlayGamesServices>
</Application>
</Manifest>
步驟 2:登入時要求 Recall 存取權
當遊戲處理登入流程時 (例如新增遊戲內帳戶),請使用 GamesRecallClient::RequestRecallAccess() 要求 Recall 存取權。
這項呼叫會傳回工作階段 ID,後端會使用該 ID 向 Google 進行伺服器端呼叫,以便連結及取消連結遊戲內帳戶與 Play 遊戲服務使用者。
auto promise = std::make_shared<std::promise<RecallAccessResult>>();
games_recall_client.RequestRecallAccess(params, [promise](RecallAccessResult result) {
promise->set_value(std::move(result));
});
auto recall_access_result = promise->get_future().get();
if (recall_access_result.ok()) {
auto recall_session_id = recall_access_result.value().recall_session_id;
// Pass the recall session ID to your backend game server so it can query
// for an existing linked in-game account.
// - If you discover an existing linked in-game account, continue to sign-in
// the in-game account. This provides a seamless cross-device sign-in
// experience.
// - If there is not an existing linked in-game account, when the user
// completes the sign-in using your in-game account system record the
// account linking with Play Games Services Recall. This helps to provide
// a seamless cross-device sign-in experience when the user returns on a
// different device or after re-installing your game on the same device.
} else {
// Handle the error
}
步驟 3:處理 Recall 工作階段 ID
遊戲取得 Recall 工作階段 ID 並傳送至後端遊戲伺服器後,請使用 Play 遊戲伺服器端 REST API 執行下列操作:
- 使用
recall.retrieveTokens查詢現有連結的遊戲內帳戶 - 使用
recall.linkPersona新增或更新連結的遊戲帳戶 - 使用
recall.unlinkPersona刪除已連結的遊戲內帳戶
如需伺服器端整合的詳細指南,請參閱這份文件,瞭解如何在遊戲伺服器中使用 Recall API。