# 使用匿名登录

> Implement anonymous sign-in to create new players without requiring any sign-in credentials or player input.

匿名登录会为游戏创建一个新玩家，而无需玩家输入任何信息。玩家可以通过这种方法快速开启游戏。

如果会话令牌已经缓存到 SDK 上，请使用 `SignInAnonymouslyAsync()` 方法恢复缓存玩家的已有凭据，无论之前是匿名登录还是通过平台帐户登录。如果没有玩家登录信息，此方法会创建新的匿名玩家。

**注意**：如果是匿名帐户且未关联到平台特定帐户，则无法恢复该帐户。例如，如果玩家卸载并重新安装游戏，则无法再登录其匿名帐户。

以下代码示例展示如何为玩家设置匿名登录功能并获取访问令牌。

```cs
async Task SignInAnonymouslyAsync()
{
    try
    {
        await AuthenticationService.Instance.SignInAnonymouslyAsync();
        Debug.Log("Sign in anonymously succeeded!");
        
        // Shows how to get the playerID
        Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); 

    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
     }
}
```

成功执行匿名登录后，建议将其关联到至少一家受支持的[身份提供商](./approaches-to-authentication.md)，让您的玩家能够在需要时使用其他设备继续游戏或恢复其帐户。
