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