使用 Kongregate 和 Unity 设置 PlayFab 身份验证

本教程演示使用 Kongregate 和 Unity 在 PlayFab 对玩家进行身份验证所需的最少设置。

要求

设置 Kongregate 应用

Kongregate 要求先上传应用的预览版本,然后才能访问所需应用信息。 第一步是准备包含以下所示内容的 index.html 文件。

<!doctype html>
<html lang="en-us">
<head></head>
<body>
 <h1>Placeholder</h1>
</body>
</html>

导航到 Kongregate 网站

  1. 选择 游戏 选项卡。
  2. 然后选择 上传游戏 按钮。

Kongregate“Games”标签页

用于设置新应用程序的页面会打开。

  1. 确保输入应用程序名称
  2. 输入 游戏说明
  3. 然后选择 类别
  4. 通过选择 继续 按钮提交新应用。

Kongregate 上传游戏

将转到 Application Upload 页面。

首先(并且最重要的是)确保将 Web 地址栏中的 URL 保存在安全且易于访问的位置。 当您在关闭该页面后还原对应用程序的访问时,这会节省大量时间

  1. 此操作完成后,选择准备好的 index.html 文件作为 游戏文件
  2. 然后设置屏幕大小。
  3. 确保接受所有必需的许可证。
  4. 最后,通过选择 上传 按钮上传应用程序。

Kongregate 应用程序上传页面

预览打开后,忽略内容并打开 api information 链接。

Kongregate 预览 API 信息

API Information 页面打开后,找到 API Key 并将它保存在安全且易于访问的位置,以供以后使用。

Kongregate API 密钥

配置 PlayFab 游戏

在 PlayFab 游戏 Game Manager 中:

  1. 从左侧菜单中选择 加载项
  2. 找到并选择 Kongregate 图标。

PlayFab 选择 Kongregate 加载项

一个新页面会打开,用于设置 Kongregate 集成:

  1. 输入在上一节中获取的 API 密匙
  2. 选择 安装 Kongregate 按钮。

PlayFab 设置 Kongregate 集成

此时,如果未收到错误消息,则表示已正确配置了 PlayFab 游戏与 Kongregate 应用程序的集成。

设置 Unity 项目

使用此 Unity 设置清单:

Unity Assets/WebGL Templates 文件夹

现在创建工作场景。

  1. 出于测试目的,请使用带有几个文本标签的屏幕缩放画布。

注意

我们只需一个 文本标签来显示调试消息。

  1. 创建一个空 GameObject,将它重命名为 Kongregate

Unity BootScene

GameObject 包含一个 KongregateHandler 组件,其中连接了一个用于显示调试消息的文本标签。

Unity GameObject Kongregate 处理程序

下面提供了 KongregateHandler 组件的代码。

// We are specifically interested in importing PlayFab related namespaces
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using UnityEngine.UI;

public class KongregateHandler : MonoBehaviour
{
  
    // Standard unity callback, executed once for the script to start
    public void Start()
    {
        // Utility: show feedback
        SetMessage("Loading kongregate api...");

        /*
         * Important: execute Javascript in the external context to initialize
         * Kongregate API, Unity Support and set up callback GameObject and Method.
         * In this case, callback is set to a GameObject called Kongregate and a
         * method called OnKongregateAPILoaded, which we define later in this class.
         * Once Kongregate API is initialized, Unity will locate this object by name
         * ("Kongregate") and execute a method "OnKongregateAPILoaded" passing in user
         * info string as an argument.
         */
        Application.ExternalEval(
          "if(typeof(kongregateUnitySupport) != 'undefined'){" +
          " kongregateUnitySupport.initAPI('Kongregate', 'OnKongregateAPILoaded');" +
          "} else {" +
          " console.error('No unity support!');" +
          "};"
        );
    }

    /*
     * Executed once Kongregate API is ready. This method is invoked by KongregateAPI
     * and receives a structured text with multiple pieces of data you must parse manually.
     * The userInfo string parameter has the following structure: 'user_identifier|user_name|auth_token'
     */
    public void OnKongregateAPILoaded(string userInfo)
    {
        SetMessage("Received user info! Logging though playfab...");

        // We split userInfo string using '|' character to acquire auth token and Kongregate ID.
        var userInfoArray = userInfo.Split('|');
        var authTicket = userInfoArray[2];
        var kongregateId = userInfoArray[0];

        LogToBrowser("Auth Token: " + authTicket);
        LogToBrowser("Kongregate Id: " + kongregateId);

        /*
         * We then execute PlayFab API call called LoginWithKongregate.
         * LoginWithKongregate requires KongregateID and AuthTicket.
         * We also pass CreateAccount flag, to automatically create player account.
         */
        PlayFabClientAPI.LoginWithKongregate(new LoginWithKongregateRequest
        {
            KongregateId = kongregateId,
            AuthTicket = authTicket,
            CreateAccount = true
        }, OnLoggedIn, OnFailed);
    }


    /*
     * The rest of the code serves as a utility to process results, log debug statements
     * and display them using Text message label.
     */

    private void OnLoggedIn(LoginResult obj)
    {
        SetMessage("Logged in through PlayFab!");
    }
    private void OnFailed(PlayFabError error)
    {
        SetMessage("Failed to login in with PlayFab: " + error.GenerateErrorReport());
    }

    private void SetMessage(string message)
    {
        InfoLabel.text = message;
    }

    private void LogToBrowser(string message)
    {
        Application.ExternalEval(string.Format("console.log('{0}')", message));
    }

    public Text InfoLabel;
}

测试

可以真正测试集成的唯一方法是将原型上传到 Kongregate。

  1. 打开 生成 窗口,并确保从菜单中选择 WebGL
  2. 然后,选择相应按钮打开 玩家设置

Kongregate 生成设置

玩家设置屏幕中,找到 设置 WebGL内部版本:

  1. 验证是否正确设置了大小。
  2. 然后选择 Kongregate Preloader 模板

Kongregate WebGL 设置

生成应用程序,然后打开 Build 文件夹。

  • 最后会得到 index.html 以及一批其他文件(具体取决于 Unity 版本)。
  • index.html 以外的所有文件压缩为一个 zip 存档,如下面提供的示例所示。

Unity build 文件夹

使用之前保存的 URL 访问 Application Upload 页面。

  1. 选择 index.html 作为 游戏文件
  2. 然后选择 zip 存档作为 其他文件
    • 接受许可证并上传。

Kongregate 应用程序上传页面—添加文件

预览打开后,随着消息的变化可以实时查看游戏。

完成时,消息会指示已通过 PlayFab 成功登录。

此时已成功地将 PlayFab 与 Kongregate 集成。