Play 游戏服务 Recall

在继续使用您自己的账号系统的同时,让用户顺畅登录您的游戏。借助 Play 游戏服务 Recall API,您可以将游戏内账号与 Google Play 游戏服务账号相关联。然后,当用户在不同设备(或重新安装游戏后在同一设备)上玩游戏时,您可以查询关联的游戏内账号,并简化登录流程。

如果您已与 Android Recall API 集成,那么您应该对这些 Recall API 比较熟悉。与 Play 游戏服务 Recall 进行的任何服务器端集成都可以供 PC 版游戏重复使用,因为它们在 Android 和 PC 上是相同的。

命名空间PlayPcSdkManaged.Recall

客户端类RecallClient

前提条件

在清单中添加 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 游戏启动它。如需了解其他清单配置步骤,请参阅 开发者模式设置指南

创建客户端

始终使用工厂创建 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 API 的 文档