メインコンテンツまでスキップ

Steam ログインに SDK を使用する - Steam でログインする - (Unity モジュール)

Last updated on May 22, 2025
備考

This tutorial module does not apply to WebGL builds due to limitations in Steamworks.

Unwrap the wrapper

This tutorial shows you how to use the AccelByte Gaming Services (AGS) Game SDK to log in with Steam session token. That way, players do not need to manually enter credentials when logging in to the game, but instead can rely on their Steam credentials to login automatically.

This tutorial uses a wrapper class named SinglePlatformAuthWrapper_Starter that acts as the wrapper to cache and handle login-related functionalities when using the AGS Game SDK and Steamworks.NET-related functions.

The class have a variable to hold optional parameter that will be used when calling the login function. This is necessary if you want to integrate login queue later on. But it can be used even if you're not planning on implementing login queue. Simply left the parameter unset.

// Optional Parameters
public LoginV4OptionalParameters OptionalParameters = new();
  • SinglePlatformAuthWrapper_Starter file: Assets/Resources/Modules/SinglePlatformAuth/Scripts/SinglePlatformAuthWrapper_Starter.cs

Implement logins using AGS Game SDK and Steam Session tokens

You will continue the Steam login implementation within SinglePlatformAuthWrapper_Starter.

  1. You will use steamAuthSessionTicket as a platformToken and show retry pop-up if it somehow fails to get steamAuthSessionTicket. Enter the code below into SinglePlatformAuthWrapper_Starter. The platformOptionParameters variable, as the name suggest, is an optional parameters that is necessary to integrate login queue if you decide to implement it later.

    private void OnGetAuthSessionTicketFinished(string steamAuthSessionTicket)
    {
    if (loginHandler == null) return;
    if (String.IsNullOrEmpty(steamAuthSessionTicket))
    {
    loginHandler.OnLoginCompleted(
    CreateLoginErrorResult(ErrorCode.CachedTokenNotFound,
    "Failed to get steam token"));
    }
    else
    {
    // Casting doesn't work properly, manually transfer the data instead.
    LoginWithOtherPlatformV4OptionalParameters platformOptionParameters = new LoginWithOtherPlatformV4OptionalParameters
    {
    OnQueueUpdatedEvent = OptionalParameters.OnQueueUpdatedEvent,
    OnCancelledEvent = OptionalParameters.OnCancelledEvent,
    CancellationToken = OptionalParameters.CancellationToken
    };

    // Login with platform token
    user.LoginWithOtherPlatformV4(
    platformType,
    steamAuthSessionTicket,
    platformOptionParameters,
    OnLoginWithOtherPlatformCompleted);
    }
    }
  2. Upon success of a login with another platform, you will get the public user data, such as an avatar URL and a display name. You cache TokenData since you will call LoginHandler.OnLoginCompleted later when you have successfully obtained the public user data. Enter the code below into SinglePlatformAuthWrapper_Starter.

    private void OnLoginWithOtherPlatformCompleted(Result<TokenData, OAuthError> result)
    {
    if (result.IsError)
    {
    loginHandler.OnLoginCompleted(result);
    }
    else
    {
    tokenData = result.Value;
    GetUserPublicData(tokenData.user_id);
    }
    }

    private void GetUserPublicData(string receivedUserId)
    {
    GameData.CachedPlayerState.PlayerId = receivedUserId;
    user.GetUserOtherPlatformBasicPublicInfo("ACCELBYTE", new string[] { receivedUserId }, OnGetUserPublicDataFinished);
    }
  3. Once the public user data has been successfully acquired, you can complete the Steam login flow and show the main menu. You can complete it by calling LoginHandler.OnLoginCompleted and pass the cached TokenData. Enter the code below into SinglePlatformAuthWrapper_Starter.

    private void OnGetUserPublicDataFinished(Result<AccountUserPlatformInfosResponse> result)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning($"Failed to get user public data info. Error: {result.Error.Message}");
    loginHandler.OnRetryLoginClicked = () => GetUserPublicData(tokenData.user_id);
    loginHandler.OnLoginCompleted(CreateLoginErrorResult(result.Error.Code, result.Error.Message));
    }
    else
    {
    AccountUserPlatformData publicUserData = result.Value.Data[0];
    string truncatedUserId = publicUserData.UserId[..5];
    GameData.CachedPlayerState.AvatarUrl = publicUserData.AvatarUrl;
    GameData.CachedPlayerState.PlayerName = string.IsNullOrEmpty(publicUserData.DisplayName) ?
    $"Player-{truncatedUserId}" : publicUserData.DisplayName;
    GameData.CachedPlayerState.PlatformId = string.IsNullOrEmpty(GameData.CachedPlayerState.PlatformId) ?
    tokenData.platform_id : GameData.CachedPlayerState.PlatformId;

    loginHandler.OnLoginCompleted(Result<TokenData,OAuthError>.CreateOk(tokenData));
    }
    }
  4. Add the CreateLoginErrorResult() function, which is needed on login complete to handle the error scenarios. Enter the code below into SinglePlatformAuthWrapper_Starter.

    private Result<TokenData, OAuthError> CreateLoginErrorResult(ErrorCode errorCode, string errorDescription)
    {
    return Result<TokenData, OAuthError>.CreateError(new OAuthError()
    {
    error = errorCode.ToString(),
    error_description = errorDescription
    });
    }

Resources