开始使用
Follow this workflow to create a Unity account, link your project, and install the SDK to get started with Unity Authentication.
阅读时间2 分钟最后更新于 1 个月前
为了使用 Authentication,您需要:
- 注册 UGS。
- 将后台关联到 Unity 编辑器项目。
- 在您的游戏代码中安装 SDK。
- 初始化 SDK。
注册
如果您还没有 Unity 帐户,创建一个帐户并创建一个新项目,以注册使用 Unity Gaming Services(Unity 游戏服务)。- 登录 Unity Dashboard。
- 在侧面板中选择 Administration(管理)。
- 选择顶部横幅中的 Sign Up(注册) 并按照说明操作。
关联您的项目
在 Unity 编辑器中通过 Project ID 将您的项目关联到 Unity Cloud 项目。通过以下步骤获取 Project ID。-
在 Unity 编辑器菜单中,选择 Edit(编辑)> Project Settings(项目设置)> Services(服务) 选项卡。

- 如果您尚未登录您的 Unity ID,请创建新的 Unity ID 或者登录。
- 要创建新的项目,请选择您的组织,然后选择 Create(创建)。要关联到现有的项目,请选择 I already have a Unity Project ID(我已经有 Unity Project ID)。接下来,从下拉选单中选择您的组织和项目,然后选择 Link(关联)。
安装 SDK
为 Unity 安装全新的 Authentication 软件包:- 在 Unity 编辑器中,打开 Window(窗口)> Package Manager(包管理器)。
- 在 Package Manager(包管理器)中,选择 Unity Registry(Unity 注册表) 列表视图。
- 搜索 Authentication,或者在软件包列表中查找。
- 选择该包,然后选择 Install(安装)。
初始化 Unity Services SDK
要在您的游戏中实现 Unity Authentication,请按照以下代码片段初始化项目中包含的所有 Unity Services SDK:using System;using Unity.Services.Core;using UnityEngine;public class InitializationExample : MonoBehaviour{ async void Awake() { try { await UnityServices.InitializeAsync(); } catch (Exception e) { Debug.LogException(e); } }}
注册身份验证事件
要接收有关您玩家状态的更新信息,请将功能注册到SignedInSignInFailedSignedOut// Setup authentication event handlers if desiredvoid SetupEvents() { AuthenticationService.Instance.SignedIn += () => { // Shows how to get a playerID Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); // Shows how to get an access token Debug.Log($"Access Token: {AuthenticationService.Instance.AccessToken}"); }; AuthenticationService.Instance.SignInFailed += (err) => { Debug.LogError(err); }; AuthenticationService.Instance.SignedOut += () => { Debug.Log("Player signed out."); }; AuthenticationService.Instance.Expired += () => { Debug.Log("Player session could not be refreshed and expired."); };}