Skip to main content

Add a login menu - Login with Steam - (Unity module)

Last updated on November 25, 2024
info

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

What's on the menu

In this tutorial, you will learn about the user interface (UI) that has prepared to perform logins with single platform authentication (Steam).

The UI is a button that, when clicked, will perform logins with Steam. The required files for this button are in the Resources section and consist of the following:

  • SinglePlatformAuthWrapper_Starter.cs: A C# class that contains most of the implementation.
    • CS file: Assets/Resources/Modules/SinglePlatformAuth/Scripts/SinglePlatformAuthWrapper_Starter.cs
  • LoginMenuCanvas.prefab: A Canvas prefab that you configured in the device ID login module.
    • Prefab file: Assets/Resources/Modules/AuthEssentials/Prefabs/LoginMenuCanvas.prefab

The LoginMenuCanvas contains a deactivated Steam login button as shown in the image below:

login menu canvas with additional login with steam button Unity Byte Wars Steam

You will guided through how SinglePlatformAuthWrapper.cs is built, and you can follow it by coding inside SinglePlatformAuthWrapper_Starter.cs.

Check inside SinglePlatformAuthWrapper_Starter.cs. It contains this function:

    private void SetLoginWithSteamButtonClickCallback()
{
// TODO: this function will get login with steam button reference and assign onClick callback to it
}

Ready the UI

The LoginMenuCanvas.prefab you learned about in device ID has several login states: in-progress, failed, and successful.

LoginMenuCanvas.prefab has a Steam login button, but it is hidden. You will use this button in the SinglePlatformAuthWrapper_Starter class.

  1. With the project open in Unity, open Assets/Resources/Modules/TutorialModuleConfig.json and activate the Steam login button using the code below.

    {
    "enableModulesOverride": true,
    "forceEnabledModules": ["AuthEssentials"],
    "forceDisabledOtherModules" : false,
    "steamConfiguration":
    {
    "steamAppId": "replace with your steam app id",
    "autoLogin": false
    }
    }
  2. Open the SinglePlatformAuthWrapper_Starter class and add the code below. This code will get the button reference and set its activation based on whether the module is active and whether the game is launched from Steam.

    private void SetLoginWithSteamButtonClickCallback()
    {
    if (loginHandler == null)
    {
    if (MenuManager.Instance != null)
    {
    var menuCanvas = MenuManager.Instance.GetMenu(AssetEnum.LoginMenuCanvas);
    if (menuCanvas != null && menuCanvas is LoginHandler loginHandlerC)
    {
    loginHandler = loginHandlerC;
    var loginWithSteamButton = loginHandler.GetLoginButton(LoginType.Steam);
    bool isSingleAuthModuleActive =
    TutorialModuleManager.Instance
    .IsModuleActive(TutorialType.SinglePlatformAuth);
    bool isLoginWithSteam = SteamManager.Initialized && isSingleAuthModuleActive;
    if (isLoginWithSteam)
    {
    loginWithSteamButton.onClick
    .AddListener(OnLoginWithSteamButtonClicked);
    loginWithSteamButton.gameObject.SetActive(true);
    }
    }
    }
    }
    }
  3. Open Assets/Resources/Modules/SinglePlatformAuth/SinglePlatformAuthAssetConfig.asset from the Project window and, in the Inspector, enable Is Starter Active.

    enable starter module for login with steam module Unity Byte Wars Steam

  4. Ensure Steam is open on your computer and play the game. If implemented correctly, you will the login with Steam button.

Implement auto Steam login

In this section, you will implement logging in with Steam automatically when the game launches.

  1. Included with the Byte Wars project is a setting in Assets/Resources/Modules/TutorialModuleConfig.json called autoLogin that acts as a toggle for whether the game should perform auto login or show the login menu. Change this to true.

    {
    "enableModulesOverride": true,
    "forceEnabledModules": ["AuthEssentials"],
    "forceDisabledOtherModules" : false,
    "steamConfiguration":
    {
    "steamAppId": "replace with your steam app id",
    "autoLogin": true
    }
    }
  2. Open the SinglePlatformAuthWrapper_Starter.cs class. In SetLoginWithSteamButtonClickCallback(), add the following code:

    private void SetLoginWithSteamButtonClickCallback()
    {
    if (loginHandler == null)
    {
    if (MenuManager.Instance != null)
    {
    var menuCanvas = MenuManager.Instance.GetMenu(AssetEnum.LoginMenuCanvas);
    if (menuCanvas != null && menuCanvas is LoginHandler loginHandlerC)
    {
    loginHandler = loginHandlerC;
    var loginWithSteamButton = loginHandler.GetLoginButton(LoginType.Steam);
    bool isSingleAuthModuleActive =
    TutorialModuleManager.Instance
    .IsModuleActive(TutorialType.SinglePlatformAuth);
    bool isLoginWithSteam = SteamManager.Initialized && isSingleAuthModuleActive;
    if (isLoginWithSteam)
    {
    if (GConfig.GetSteamAutoLogin())
    {
    OnLoginWithSteamButtonClicked();
    }
    else
    {
    loginWithSteamButton.onClick.AddListener(OnLoginWithSteamButtonClicked);
    loginWithSteamButton.gameObject.SetActive(true);
    }
    }
    }
    }
    }
    }
  3. Compile your project and make sure there are no errors.

Resources