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

すべてを統合する - Steam でログインする - (Unity モジュール)

Last updated on June 23, 2025
備考

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

Connect the UI to Let the Player Log In with Steam

In this tutorial, you will learn how to connect the login menu with the Steam login implementation created in the previous section.

  1. Open the SinglePlatformAuthWrapper_Starter class and add the following function. This function sends a request to Steam to obtain an auth ticket, which is then used as a platform token to log in to AGS. Make sure to wrap it with the #if !UNITY_WEBGL since Steam does not supported for WebGL builds.

    #if !UNITY_WEBGL
    private async void GetSteamAuthTicket(ResultCallback<string> resultCallback)
    {
    if (!SteamManager.Initialized)
    {
    BytewarsLogger.LogWarning(
    "Failed to get Steam auth ticket. Steam API is not initialized. " +
    "Try to open the Steam Client first before launching the game.");
    resultCallback?.Invoke(Result<string>.CreateError(ErrorCode.NotImplemented, "Steam API is not initialized"));
    return;
    }

    byte[] buffer = new byte[1024];
    SteamNetworkingIdentity identity = new SteamNetworkingIdentity();
    identity.SetGenericString(string.Empty);

    // Request to get Steam auth ticket.
    HAuthTicket request = SteamUser.GetAuthSessionTicket(buffer, buffer.Length, out uint ticketSize, ref identity);
    Array.Resize(ref buffer, (int)ticketSize);

    // Set request callback.
    TaskCompletionSource<string> getAuthTicketTask = new TaskCompletionSource<string>();
    Callback<GetAuthSessionTicketResponse_t> callback = Callback<GetAuthSessionTicketResponse_t>.Create((GetAuthSessionTicketResponse_t response) =>
    {
    if (response.m_hAuthTicket == request && response.m_eResult == EResult.k_EResultOK)
    {
    string sessionTicket = BitConverter.ToString(buffer).Replace("-", string.Empty);
    getAuthTicketTask.TrySetResult(sessionTicket);
    }
    else
    {
    getAuthTicketTask.TrySetResult(null);
    }
    });

    // Return Steam auth ticket.
    string authTicket = await getAuthTicketTask.Task;
    if (string.IsNullOrEmpty(authTicket))
    {
    resultCallback?.Invoke(Result<string>.CreateError(ErrorCode.UnknownError, "Failed to get Steam Auth Ticket"));
    }
    else
    {
    resultCallback?.Invoke(Result<string>.CreateOk(authTicket));
    }
    }
    #endif
  2. Replace the OnLoginWithSteamButtonClicked() function with the updated code below. This code retrieves the Steam auth ticket and uses it as a platform token to log in to AGS. After the login attempt, the login menu handles the result—either redirecting the player to the main menu or displaying an error message. Make sure to wrap it with the #if !UNITY_WEBGL since Steam does not supported for WebGL builds.

    #if !UNITY_WEBGL
    private void OnLoginWithSteamButtonClicked()
    {
    LoginMenu_Starter loginMenu = MenuManager.Instance.GetCurrentMenu() as LoginMenu_Starter;
    if (!loginMenu)
    {
    return;
    }

    loginMenu.OnRetryLoginClicked = OnLoginWithSteamButtonClicked;
    loginMenu.WidgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);

    // Get Steam token to be used as platform token
    GetSteamAuthTicket((Result<string> result) =>
    {
    if (result.IsError)
    {
    loginMenu.OnLoginCompleted(Result<TokenData, OAuthError>.CreateError(new OAuthError() { error = result.Error.Message }));
    return;
    }

    // Log in to AccelByte with the Steam auth ticket
    LoginWithOtherPlatform(result.Value, PlatformType.Steam, loginMenu.OnLoginCompleted);
    });
    }
    #endif

Resources