Documentation
Ask or search…
K
Links

Sign in via Unity Gaming Services

Signing in the Hide-and-seek game via Unity Game Services (UGS) Authentication
Please start by following the Unity Gaming Services Setup guide.
Create a UI Menu for Sign in, including text for the username, a sign-in button, and a status text.
In MenuSceneManager.cs, add this function to Sign in:
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Lobbies;
// ...
public async void SignInButtonClicked()
{
if (string.IsNullOrEmpty(_nameTextField.text))
{
Debug.Log($"Signing in with the default profile");
await UnityServices.InitializeAsync();
}
else
{
Debug.Log($"Signing in with profile '{_nameTextField.text}'");
var options = new InitializationOptions();
options.SetProfile(_nameTextField.text);
await UnityServices.InitializeAsync(options);
}
try
{
signInButton.interactable = false;
statusText.text = $"Signing in...";
AuthenticationService.Instance.SignedIn += delegate
{
PlayerDataManager.Instance.SetId(AuthenticationService.Instance.PlayerId);
UpdateStatusText();
profileMenu.SetActive(false);
lobbyMenu.SetActive(true);
};
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
catch (Exception e)
{
signInButton.interactable = true;
statusText.text = $"Sign in failed : {e.ToString()} ";
Debug.LogException(e);
throw;
}
}
Within the function, if you lack any variables, declare and reference them as needed.
In the Awake method, initialize Unity Services.
UnityServices.InitializeAsync();
In the MenuSceneManager, in the Start function, call CheckAuthentication to determine whether the user is signed in or not, particularly when transitioning back from the Play Scene to the Menu Scene.
void CheckAuthentication()
{
/* Check signed in */
if (AuthenticationService.Instance.IsSignedIn)
{
UpdateStatusText();
profileMenu.SetActive(false);
lobbyMenu.SetActive(true);
}
else
{
profileMenu.SetActive(true);
lobbyMenu.SetActive(false);
}
}