using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;
public static class AuthenticationManager
{
#region Initialization
///
/// Static method called before scene load to initialize Unity Services and begin authentication flow.
///
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static async void Initialize()
{
try
{
await UnityServices.InitializeAsync();
}
catch (Exception e)
{
Debug.LogError(e);
}
}
///
/// Automatically attempts sign-in based on stored provider type after Unity Services initialization.
///
#endregion
#region Authentication Flow
///
/// Attempts to sign in anonymously as a guest account and sets basic user data.
///
/// A Task representing the asynchronous operation.
public static async Task SigninAnonymously()
{
try
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
catch (Exception e)
{
Debug.LogError(e);
}
}
///
/// Attempts to sign in using a username and password. Loads user data from cloud save.
///
/// The username of the account.
/// The password of the account.
/// A Task representing the asynchronous operation.
public static async Task SigninUsernamePassword(string userName, string password)
{
try
{
await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(userName, password);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
///
/// Registers a new account using username, password, email, and province information.
///
/// The desired username.
/// The desired password.
/// The user's email address.
/// The user's province or region.
/// A Task representing the asynchronous operation.
public static async Task SignUpUsernamePassword(string userName, string password, string email, string age)
{
try
{
await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync(userName, password);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
///
/// Links the current anonymous account to a username/password account by adding credentials and updating user data.
///
/// The username to link.
/// The password to link.
/// The user's email address.
/// The user's province or region.
/// A Task representing the asynchronous operation.
public static async Task LinkAccountUsernamePassword(string userName, string password, string email, string age)
{
try
{
await AuthenticationService.Instance.AddUsernamePasswordAsync(userName, password);
}
catch (Exception e)
{
Debug.LogError(e);
}
}
#endregion
#region Utilities
///
/// Signs out the current user and deletes the anonymous session if applicable.
///
public static void Signout()
{
AuthenticationService.Instance.SignOut(true);
}
#endregion
}