Steam
Provide a Steam sign-in option to enable players to authenticate using their Steam accounts in your game.
Read time 3 minutesLast updated 15 hours ago
Minimum SDK version: 2.0.0
This article guides you through the following scenarios in setting up authentication for players in your game with a Steam account:- Set up a Steam account sign-in.
- Sign in a returning user or create new user.
- Updating a user from an anonymous sign-in to a platform sign-in via a Steam account.
Set up a Steam account sign-in
- Create your app and note down the app ID following the Steamworks documentation.
- Create the Publisher Web API key following the Authentication using Web API Keys documentation.
-
Install the SteamWorks.Net SDK following instructions to install the SDK specifically for Unity.
- Copy the SteamWorks.Net SDK into your project’s folder.
Assets/ - Open the in the root of your Unity project and replace 480 with your own App ID.
steam_appid.txt - Add the Steam Manager game component to a game object in the scene. It will initialize the steam library.
- Before you test, make sure Steam is installed and logged in. The Steam user has the game you are developing in the library.
- Copy the SteamWorks.Net SDK into your project’s
-
Configure your ID provider to be Steam for Unity Authentication:
- In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication from the navigation menu.
- Set ID Providers to Steam, then select Add.
- Enter the app ID in the App ID text field.
- Enter the Publisher Web API Key in the Key text field, then select Save.
- (Optional) Add Additional App IDs for Demo, PlayTest, Alpha builds, still in the Authentication Settings:
- Select the arrow next to Additional App ID.
- Select Add App ID at the bottom of the expanded view.
- Enter the App ID and a Description.
- Select Save.
- (Optional) Add Additional App IDs for Demo, PlayTest, Alpha builds, still in the Authentication Settings:
- Implement the Steam login using the sample code below. Refer to the documentation of GetAuthTicketForWebApi. Unity Authentication SDK accepts only Steam session tickets. Encrypted Application Tickets are not accepted.
Callback<GetTicketForWebApiResponse_t> m_AuthTicketForWebApiResponseCallback;string m_SessionTicket;string identity = "unityauthenticationservice";void SignInWithSteam(){ // It's not necessary to add event handlers if they are // already hooked up. // Callback.Create return value must be assigned to a // member variable to prevent the GC from cleaning it up. // Create the callback to receive events when the session ticket // is ready to use in the web API. // See GetAuthSessionTicket document for details. m_AuthTicketForWebApiResponseCallback = Callback<GetTicketForWebApiResponse_t>.Create(OnAuthCallback); SteamUser.GetAuthTicketForWebApi(identity);}void OnAuthCallback(GetTicketForWebApiResponse_t callback){ m_SessionTicket = BitConverter.ToString(callback.m_rgubTicket).Replace("-", string.Empty); m_AuthTicketForWebApiResponseCallback.Dispose(); m_AuthTicketForWebApiResponseCallback = null; Debug.Log("Steam Login success. Session Ticket: " + m_SessionTicket); // Call Unity Authentication SDK to sign in or link with Steam, displayed in the following examples, using the same identity string and the m_SessionTicket.}
Sign in a returning player or create new player
Use theSignInWithSteamAsync- Create a new Unity Authentication player with the Steam credentials.
- Sign in an existing player using the Steam credentials.
SignInWithSteamAsyncSignInWithSteamAsyncSignInWithSteamAsyncasync Task SignInWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
Update a player from anonymous to a Steam account
After anonymous authentication set up, if the player wants to upgrade from being anonymous to create a Steam account and sign in using a Steam account, the game should prompt the player to trigger the Steam sign-in and get the session ticket from Steam. Then, call theLinkWithSteamAsync- Sign into the cached player's account using .
SignInAnonymouslyAsync - Link the cached player's account to the Steam account with .
LinkWithSteamAsync
When the player triggers the Steam login by signing in or by creating a new player profile, and you have received the Steam access token using the same identity parameter, call the following API to authenticate the player.async Task LinkWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.LinkWithSteamAsync(ticket, identity); Debug.Log("Link is successful."); } catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked) { // Prompt the player with an error message. Debug.LogError("This user is already linked with another account. Log in instead."); } catch (Exception ex) { Debug.LogError("Link failed."); Debug.LogException(ex); }}
async Task SignInWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
Sign in a returning player or create new player for Additional App IDs
Use the sameSignInWithSteamAsyncasync Task SignInWithSteamAsync(string ticket, string identity, string appId){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity, appId); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
Unlink Steam account
Use theUnlinkSteamAsyncasync Task UnlinkSteamAsync(){ try { await AuthenticationService.Instance.UnlinkSteamAsync(); Debug.Log("Unlink is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}