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

Implement SDK - Login Queue - (Unity module)

Last updated on May 22, 2025
注記

To integrate the login queue feature, you must use the V4 version of the AccelByte Unity SDK login functions (e.g., LoginWithDeviceIdV4() or LoginWithOtherPlatformV4()). These functions support passing an OptionalParameters object, which is required for the login queue to function properly.

The Byte Wars authentication tutorial modules already use these V4 functions. So if you’ve followed the tutorial from the beginning, you're all set.

Unwrap the wrapper

In this tutorial, you will learn how to implement the login queue and cancel login using the AccelByte Gaming Services (AGS) Game SDK. Byte Wars has a wrapper file named LoginQueueWrapper_Starter.cs which you will be using to follow along this tutorial to implement those features from scratch.

The diagram below describes how the Login Queue feature works with the SDK.

What's in the Starter Pack

The starter class LoginQueueWrapper_Starter has been provided for you to modify. It is available in the Resources section.

  • LoginQueueWrapper_Starter file: /Assets/Resources/Modules/LoginQueue/Scripts/LoginQueueWrapper_Starter.cs

There are a few things already prepared for you in this class:

  • Library to enable the usage of AccelByte SDK's object.
using AccelByte.Models;
  • Reference to other modules' wrapper. The login queue feature works by passing an OptionalParameters object containing delegates and a cancellation token to the login function. Since Byte Wars uses separate authentication modules, this class assigns the OptionalParameters to the corresponding module instead of modifying the login function directly. This allows the game to continue calling the original login functions from those modules without any changes.
private AuthEssentialsWrapper authEssentialsWrapper;
private SinglePlatformAuthWrapper singlePlatformAuthWrapper;
private AuthEssentialsWrapper_Starter authEssentialsWrapper_Starter;
private SinglePlatformAuthWrapper_Starter singlePlatformAuthWrapper_Starter;
private void Start()
{
authEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper>();
singlePlatformAuthWrapper = TutorialModuleManager.Instance.GetModuleClass<SinglePlatformAuthWrapper>();
authEssentialsWrapper_Starter = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper_Starter>();
singlePlatformAuthWrapper_Starter = TutorialModuleManager.Instance.GetModuleClass<SinglePlatformAuthWrapper_Starter>();
}

Implement Login Queue callback

  1. Open the LoginQueueWrapper_Starter file and declare a new delegate to handle the UI later.

    public Action<LoginQueueTicket> OnLoginQueued = delegate {};
  2. Create a new function called OnQueued(). This function will be triggered whenever the game receives a new login queue update. It will display the login queue menu and invoke the delegate, passing the updated queue info.

    private void OnQueued(LoginQueueTicket queueTicket)
    {
    BytewarsLogger.Log($"Login queued.");

    // Show UI if the current UI is not Login Queue
    if (MenuManager.Instance.GetCurrentMenu().GetAssetEnum() != AssetEnum.LoginQueueMenu_Starter)
    {
    MenuManager.Instance.ChangeToMenu(TutorialType.LoginQueue);
    }

    OnLoginQueued?.Invoke(queueTicket);
    }
  3. Go to the Start() function, replace the existing code with the code below. This code retrieves the authentication modules wrapper and assigns its OptionalParameters.OnQueueUpdatedEvent event with the OnQueued() function. This way, when the user logs in using those wrappers, the OnQueueUpdatedEvent will be handled by the LoginQueueWrapper_Starter class.

    private void Start()
    {
    authEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper>();
    singlePlatformAuthWrapper = TutorialModuleManager.Instance.GetModuleClass<SinglePlatformAuthWrapper>();
    authEssentialsWrapper_Starter = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper_Starter>();
    singlePlatformAuthWrapper_Starter = TutorialModuleManager.Instance.GetModuleClass<SinglePlatformAuthWrapper_Starter>();

    // Bind Queued and Canceled action
    if (authEssentialsWrapper != null)
    {
    authEssentialsWrapper.OptionalParameters.OnQueueUpdatedEvent = OnQueued;
    }
    if (singlePlatformAuthWrapper != null)
    {
    singlePlatformAuthWrapper.OptionalParameters.OnQueueUpdatedEvent = OnQueued;
    }

    // Bind Queued and Canceled action to the starter scripts
    if (authEssentialsWrapper_Starter != null)
    {
    authEssentialsWrapper_Starter.OptionalParameters.OnQueueUpdatedEvent = OnQueued;
    }
    if (singlePlatformAuthWrapper_Starter != null)
    {
    singlePlatformAuthWrapper_Starter.OptionalParameters.OnQueueUpdatedEvent = OnQueued;
    }
    }

Implement Cancel Login

  1. Open the LoginQueueWrapper_Starter file and declare a new delegate to handle the UI later.

    public Action OnLoginCanceled = delegate {};
  2. Declare a variable named cancellationTokenSource. You will use this variable to create a cancel token used to cancel login attempts later.

    private CancellationTokenSource cancellationTokenSource;
  3. Create a function called ResetAndReassignCancellationToken(). Unity’s login cancellation works by passing a cancel token and calling Cancel() on it. The token must be recreated for each login attempt to avoid issues when a player logs out or cancels and tries to log in again. Without recreating the token, the player may get stuck in the cancelled state.

    private void ResetAndReassignCancellationToken()
    {
    cancellationTokenSource = new CancellationTokenSource();

    // Set cancellation token
    if (authEssentialsWrapper != null)
    {
    authEssentialsWrapper.OptionalParameters.CancellationToken = cancellationTokenSource.Token;
    }
    if (singlePlatformAuthWrapper != null)
    {
    singlePlatformAuthWrapper.OptionalParameters.CancellationToken = cancellationTokenSource.Token;
    }

    // Set cancellation token to the starter scripts
    if (authEssentialsWrapper_Starter != null)
    {
    authEssentialsWrapper_Starter.OptionalParameters.CancellationToken = cancellationTokenSource.Token;
    }
    if (singlePlatformAuthWrapper_Starter != null)
    {
    singlePlatformAuthWrapper_Starter.OptionalParameters.CancellationToken = cancellationTokenSource.Token;
    }
    }
  4. Create a function to trigger login cancellation.

    public void CancelLogin()
    {
    cancellationTokenSource.Cancel();
    }
  5. Create a callback function to handle the response from the login cancellation. This function logs the result, triggers the delegate, and resets the cancel token for any future login attempts.

    private void OnCanceled()
    {
    BytewarsLogger.Log($"Login canceled while in queue.");

    OnLoginCanceled?.Invoke();
    ResetAndReassignCancellationToken();
    }
  6. Go to the Start() function and replace the current implementation. This code assigns the authentication modules wrapper's OptionalParameters.OnCancelledEvent event with the OnCanceled() function and set the cancel token to the OptionalParameters.

    private void Start()
    {
    authEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper>();
    singlePlatformAuthWrapper = TutorialModuleManager.Instance.GetModuleClass<SinglePlatformAuthWrapper>();
    authEssentialsWrapper_Starter = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper_Starter>();
    singlePlatformAuthWrapper_Starter = TutorialModuleManager.Instance.GetModuleClass<SinglePlatformAuthWrapper_Starter>();

    // Bind Queued and Canceled action
    if (authEssentialsWrapper != null)
    {
    authEssentialsWrapper.OptionalParameters.OnQueueUpdatedEvent = OnQueued;
    authEssentialsWrapper.OptionalParameters.OnCancelledEvent = OnCanceled;
    }
    if (singlePlatformAuthWrapper != null)
    {
    singlePlatformAuthWrapper.OptionalParameters.OnQueueUpdatedEvent = OnQueued;
    singlePlatformAuthWrapper.OptionalParameters.OnCancelledEvent = OnCanceled;
    }

    // Bind Queued and Canceled action to the starter scripts
    if (authEssentialsWrapper_Starter != null)
    {
    authEssentialsWrapper_Starter.OptionalParameters.OnQueueUpdatedEvent = OnQueued;
    authEssentialsWrapper_Starter.OptionalParameters.OnCancelledEvent = OnCanceled;
    }
    if (singlePlatformAuthWrapper_Starter != null)
    {
    singlePlatformAuthWrapper_Starter.OptionalParameters.OnQueueUpdatedEvent = OnQueued;
    singlePlatformAuthWrapper_Starter.OptionalParameters.OnCancelledEvent = OnCanceled;
    }

    ResetAndReassignCancellationToken();
    }

Resources