Add a login menu - Login with Steam - (Unity module)
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
- CS file:
- LoginMenuCanvas.prefab: A Canvas prefab that you configured in the device ID login module.
- Prefab file:
Assets/Resources/Modules/AuthEssentials/Prefabs/LoginMenuCanvas.prefab
- Prefab file:
The LoginMenuCanvas contains a deactivated Steam login button as shown in the image below:
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.
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
}
}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);
}
}
}
}
}Open
Assets/Resources/Modules/SinglePlatformAuth/SinglePlatformAuthAssetConfig.asset
from the Project window and, in the Inspector, enableIs Starter Active
.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.
Included with the Byte Wars project is a setting in
Assets/Resources/Modules/TutorialModuleConfig.json
calledautoLogin
that acts as a toggle for whether the game should perform auto login or show the login menu. Change this totrue
.{
"enableModulesOverride": true,
"forceEnabledModules": ["AuthEssentials"],
"forceDisabledOtherModules" : false,
"steamConfiguration":
{
"steamAppId": "replace with your steam app id",
"autoLogin": true
}
}Open the
SinglePlatformAuthWrapper_Starter.cs
class. InSetLoginWithSteamButtonClickCallback()
, 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);
}
}
}
}
}
}Compile your project and make sure there are no errors.
Resources
- The files used in this tutorial section are available in the Unity Byte Wars GitHub repository.