Last active
January 13, 2025 16:46
-
-
Save iaincollins/8d639fb4091013b1e2116f7652f3ccdc to your computer and use it in GitHub Desktop.
Unity Player Account Sign In Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: This example does not include appropriate exception handling | |
using UnityEngine; | |
using Unity.Services.Authentication; | |
using Unity.Services.Authentication.PlayerAccounts; | |
using Unity.Services.Core; | |
public class SignInWithUnityExample : MonoBehaviour | |
{ | |
async void Awake() | |
{ | |
await UnityServices.InitializeAsync(); | |
PlayerAccountService.Instance.SignedIn += PlayerAccountSignedInEventHandler; | |
AuthenticationService.Instance.Expired += ExpiredEventHandler; | |
// Normally this function would be called after clicking a button, | |
// I'm just calling it here in Awake() as an example | |
SignIn(); | |
} | |
public async void SignIn() | |
{ | |
// Sign in returning player using the Session Token stored after sign in. | |
// Players don't need to sign in with Unity Player Account again; using | |
// this method will re-authorise them (they will keep the same Player ID). | |
if (AuthenticationService.Instance.SessionTokenExists) | |
{ | |
await AuthenticationService.Instance.SignInAnonymouslyAsync(); | |
Debug.Log($"Returning Player ID: {AuthenticationService.Instance.PlayerId}"); | |
Debug.Log($"Returning Player is SignedIn: {AuthenticationService.Instance.IsSignedIn}"); | |
Debug.Log($"Returning Player is Authorized: {AuthenticationService.Instance.IsAuthorized}"); | |
return; | |
} | |
// This launches a web browser to prompt a player to sign in | |
// If they have previously signed in on another device - or have been | |
// signed out of this device - then they will be signed back in with the | |
// same Player ID they had before. | |
if (!PlayerAccountService.Instance.IsSignedIn) | |
{ | |
await PlayerAccountService.Instance.StartSignInAsync(); | |
} | |
} | |
public async void ExpiredEventHandler() { | |
// You should only get here if a session fails to automatically be refreshed | |
// e.g. due to a network error, or a player being offline | |
// You would probably want to explicitly handle this condition, but how | |
// depends on the game. | |
if (!AuthenticationService.Instance.IsSignedIn) | |
{ | |
await AuthenticationService.Instance.SignInAnonymouslyAsync(); | |
} | |
} | |
async void PlayerAccountSignedInEventHandler() | |
{ | |
// This links accounts and signs them in using Unity Authentication, | |
// which will generate a Player ID and create a session Token which is | |
// stored in Player Prefs, which can be used to re-authorize a player. | |
await AuthenticationService.Instance.SignInWithUnityAsync(PlayerAccountService.Instance.AccessToken); | |
Debug.Log($"Player Accounts State: <b>{(PlayerAccountService.Instance.IsSignedIn ? "Signed in" : "Signed out")}</b>"); | |
Debug.Log($"Player Accounts Access token: <b>{(string.IsNullOrEmpty(PlayerAccountService.Instance.AccessToken) ? "Missing" : "Exists")}</b>\n"); | |
Debug.Log($"Player ID: {AuthenticationService.Instance.PlayerId}"); | |
Debug.Log($"Player is SignedIn: {AuthenticationService.Instance.IsSignedIn}"); | |
Debug.Log($"Player is Authorized: {AuthenticationService.Instance.IsAuthorized}"); | |
} | |
public void SignOut() | |
{ | |
AuthenticationService.Instance.ClearSessionToken(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This example doesn't have any exception handling!