使用 Kongregate 和 Unity 设置 PlayFab 身份验证
本教程演示使用 Kongregate 和 Unity 在 PlayFab 对玩家进行身份验证所需的最少设置。
要求
- 注册 Kongregate 帐户。
- 熟悉 Kongregate 开发者指南。
- 已注册的 PlayFab 游戏。
- 为游戏配置了 PlayFab 的 Unity 项目。
- 安装了 Unity WebGL 支持。
- 熟悉登录基础知识和最佳实践。
设置 Kongregate 应用
Kongregate 要求先上传应用的预览版本,然后才能访问所需应用信息。 第一步是准备包含以下所示内容的 index.html 文件。
<!doctype html>
<html lang="en-us">
<head></head>
<body>
<h1>Placeholder</h1>
</body>
</html>
导航到 Kongregate 网站。
- 选择 游戏 选项卡。
- 然后选择 上传游戏 按钮。
用于设置新应用程序的页面会打开。
- 确保输入应用程序名称。
- 输入 游戏说明。
- 然后选择 类别。
- 通过选择 继续 按钮提交新应用。
将转到 Application Upload 页面。
首先(并且最重要的是)确保将 Web 地址栏中的 URL 保存在安全且易于访问的位置。 当您在关闭该页面后还原对应用程序的访问时,这会节省大量时间。
- 此操作完成后,选择准备好的 index.html 文件作为 游戏文件。
- 然后设置屏幕大小。
- 确保接受所有必需的许可证。
- 最后,通过选择 上传 按钮上传应用程序。
预览打开后,忽略内容并打开 api information 链接。
API Information 页面打开后,找到 API Key 并将它保存在安全且易于访问的位置,以供以后使用。
配置 PlayFab 游戏
在 PlayFab 游戏 Game Manager 中:
- 从左侧菜单中选择 加载项。
- 找到并选择 Kongregate 图标。
一个新页面会打开,用于设置 Kongregate 集成:
- 输入在上一节中获取的 API 密匙。
- 选择 安装 Kongregate 按钮。
此时,如果未收到错误消息,则表示已正确配置了 PlayFab 游戏与 Kongregate 应用程序的集成。
设置 Unity 项目
使用此 Unity 设置清单:
- [可选] 安装 PlayFab 编辑器扩展。
- 在
PlayFabSharedSettings
可脚本化对象中设置TitleId
。
- 在
- 对于 Unity 5.6+,下载 Kongregate Preloader WebGL 模板,然后将它置于
Assets/WebGLTemplates
文件夹 (2) 中。 - 若 Unity 版本 < 5.6,请下载 Kongregate Preloader WebGL 模板 5.5 及更低版本,并将其置于
Assets/WebGLTemplates
文件夹 (2) 中。
现在创建工作场景。
- 出于测试目的,请使用带有几个文本标签的屏幕缩放画布。
注意
我们只需一个 文本标签来显示调试消息。
- 创建一个空 GameObject,将它重命名为 Kongregate。
此 GameObject
包含一个 KongregateHandler
组件,其中连接了一个用于显示调试消息的文本标签。
下面提供了 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。
- 打开 生成 窗口,并确保从菜单中选择 WebGL 。
- 然后,选择相应按钮打开 玩家设置。
在玩家设置屏幕中,找到 设置 WebGL内部版本:
- 验证是否正确设置了大小。
- 然后选择 Kongregate Preloader 模板。
生成应用程序,然后打开 Build 文件夹。
- 最后会得到 index.html 以及一批其他文件(具体取决于 Unity 版本)。
- 将 index.html 以外的所有文件压缩为一个 zip 存档,如下面提供的示例所示。
使用之前保存的 URL 访问 Application Upload 页面。
- 选择 index.html 作为 游戏文件。
- 然后选择 zip 存档作为 其他文件。
- 接受许可证并上传。
预览打开后,随着消息的变化可以实时查看游戏。
完成时,消息会指示已通过 PlayFab 成功登录。
此时已成功地将 PlayFab 与 Kongregate 集成。