在继续使用您自己的账号系统的同时,让用户能够顺畅地登录您的游戏。借助 Play 游戏服务 Recall API,您可以将游戏内账号与 Google Play 游戏服务账号相关联。然后,当用户在不同设备上(或在重新安装游戏后在同一设备上)玩您的游戏时,您可以查询关联的游戏内账号并简化登录流程。
如果您已与 Android Recall API 集成,那么这些 Recall API 应该很熟悉。与 Play Games Services Recall 的任何服务器端集成都可以供 PC 版游戏重复使用,因为这些集成在 Android 和 PC 上是相同的。
命名空间: PlayPcSdkManaged.Recall
客户端类: RecallClient
前提条件
阅读 Play 游戏服务 Recall API 的概览。
在 Play 管理中心内完成 Google Play 游戏服务设置。
在清单中添加 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>
注意:如果您想在 Unity 编辑器中开发时使用 PC SDK,而无需对游戏可执行文件进行数字签名或从 Google Play Games 启动游戏,如需了解其他清单配置步骤,请参阅开发者模式设置指南。
创建客户端
始终使用工厂来创建 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 游戏服务用户相关联和解除关联。
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 游戏服务器端 REST API 来执行以下操作:
- 使用
recall.retrieveTokens查询现有的关联游戏内账号 - 使用
recall.linkPersona添加或更新关联的游戏内账号 - 使用
recall.unlinkPersona删除关联的游戏内账号
如需更详细的服务器端集成指南,请参阅有关如何在游戏服务器中使用 Recall API 的文档。