登录游戏
初始化 Vivox SDK 后,可以将用户登录到分配给游戏的 Vivox 实例。
用于玩家的名称将是 Unity AuthenticationId,如果未使用 Unity Authentication,则名称将是新的 GUID。
注意:请勿在传递给 Account 构造函数的用户名中包含颁发者或域。Account 构造函数会为您处理此操作。
初始化完成后,应调用 VivoxService.Instance.LoginAsync
方法登录 Vivox。
VivoxService.Instance.LoginAsync
的可选参数是一个 LoginOptions 结构,可用于设置用户显示名称、启用文本转语音或载入被屏蔽用户列表。
如果在 LoginOptions 中设置了显示名称,此名称将对他们加入的频道中的所有用户可见;这些频道会在 VivoxService.Instance.ParticipantAddedToChannel
或 VivoxService.Instance.ParticipantRemovedFromChannel
的 VivoxParticipant 结果中将此名称作为 DisplayName 接收。
- 显示名称仅对当前会话有效,不会在 Vivox 网络中持续存在。
- 显示名称的最大长度为 127 字节。
注意:Vivox SDK 不对显示名称执行检查。游戏开发者有责任确保显示名称符合游戏规则,游戏内渲染器支持字体字符,并检查名称是否有重复、不文明和冒充现象。建议通过某些带外方法(如游戏服务器,而不是游戏客户端)检查显示名称。
以下代码的示例说明如何启动登录过程、将 DisplayName 设置为“Bob”并启用文本转语音。
using UnityEngine;
using Unity.Services.Vivox;
class LogInExample : MonoBehaviour
{
. . .
async void LoginUserAsync()
{
// For this example, the VivoxService is initialized.
var loginOptions = new LoginOptions()
{
DisplayName = "Bob",
EnableTTS = true
};
VivoxService.Instance.LoginAsync(loginOptions)
}
. . .
}
可以订阅 VivoxService.Instance.LoggedIn
和 VivoxService.Instance.LoggedOut
,以便在成功调用 LoginAsync 后获取事件。
以下代码的示例说明如何订阅 VivoxService.Instance.LoggedIn
和 VivoxService.Instance.LoggedOut
事件,以及如何处理不同 LoginState 值的函数:
using UnityEngine;
using Unity.Services.Vivox;
class LoginPropChangeExample : MonoBehaviour
{
. . .
VivoxService.Instance.LoggedIn += onLoggedIn;
VivoxService.Instance.LoggedOut += onLoggedOut;
. . .
private void onLoggedIn()
{
// Operations as needed
}
private void onLoggedOut()
{
// Operations as needed
}
}
注意:建议用户在应用程序启动时登录。如果用户可以在应用程序中登录不同帐户,则建议用户在连接游戏服务器时登录。