在繼續使用自己的帳戶系統時,順暢地讓使用者登入遊戲。您可以使用 Play 遊戲服務 Recall API,將遊戲內帳戶連結至 Google Play 遊戲服務帳戶。這樣一來,當使用者在不同裝置上玩遊戲 (或在同一部裝置上重新安裝遊戲後),您就能查詢已連結的遊戲內帳戶,簡化登入流程。
如果您已整合 Android Recall API,應該會對這些 Recall API 感到熟悉。與 Play 遊戲服務 Recall 整合的任何伺服器端,都可供電腦版遊戲重複使用,因為 Android 和電腦版遊戲的整合方式相同。
命名空間: PlayPcSdkManaged.Recall
用戶端類別: RecallClient
必要條件
閱讀 Play Games 服務 Recall API 總覽。
在 Play 管理中心完成 Google Play Games 服務設定。
在資訊清單中新增 Play 遊戲服務專案 ID
在 Play 管理中心完成 Play Games 服務設定後,遊戲現在會與 Play Games 服務專案 ID 建立關聯。使用這個專案 ID (可在 Play 管理中心的 Play Games 服務「設定」頁面中找到),更新遊戲的 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>
注意:如果您想在 Unity 編輯器中開發時使用電腦版 SDK,但不需要為遊戲可執行檔進行數位簽章,也不想從 Google Play 遊戲啟動遊戲,如需其他資訊清單設定步驟,請參閱開發人員模式設定指南。
建立用戶端
請務必使用工廠建立 RecallClient。確保系統會自動註冊 Unity 安全回呼。
using UnityEngine; using System; using System.Threading.Tasks; // Required SDK Namespaces using PlayPcSdkManaged.Recall; using PlayPcSdkManaged.Unity; public class RecallManager : MonoBehaviour { private RecallClient _recallClient; public void SetupRecall() { try { // Creates the client with the required UnityRecallCallbacksHandler _recallClient = PlayPcSdkFactory.CreateRecallClient(); Debug.Log("Recall Client created successfully."); } catch (Exception ex) { Debug.LogError($"Failed to create Recall Client: {ex.Message}"); } } private void OnDestroy() { // Always dispose of the client to clean up native C++ resources _recallClient?.Dispose(); } }
要求 Recall 存取權
當遊戲處理登入流程時 (例如新增遊戲內帳戶),請使用 RequestRecallAccessAsync 要求 Recall 存取權。
這項呼叫會傳回工作階段 ID,後端會使用這個 ID 向 Google 進行伺服器端呼叫,以便將遊戲內帳戶連結至 Play Games 服務使用者,或取消連結。
public async Task RequestRecallAccessAsync() { try { Debug.Log("Requesting Recall access..."); // Async call to retrieve the session ID var result = await _recallClient.RequestRecallAccessAsync(); if (result.IsOk) { // On success, access the RecallSessionId var sessionId = result.Value.RecallSessionId; Debug.Log($"Recall Access Granted! Session ID: {sessionId}"); // Pass 'sessionId' to your backend server to process account linking } else { // Handle expected API errors (e.g., Error) Debug.LogError($"Request Failed: {result.Code} - {result.ErrorMessage}"); } } catch (Exception ex) { Debug.LogException(ex); } }
處理 Recall 工作階段 ID
遊戲取得 Recall 工作階段 ID 並傳送至後端遊戲伺服器後,請使用 Play Games 伺服器端 REST API 執行下列操作:
- 使用
recall.retrieveTokens查詢現有連結的遊戲內帳戶。 - 使用
recall.linkPersona新增或更新連結的遊戲帳戶 - 使用
recall.unlinkPersona刪除已連結的遊戲內帳戶
如需伺服器端整合的詳細指南,請參閱這份文件,瞭解如何在遊戲伺服器中使用 Recall API。