Added game state and network client
This commit is contained in:
46
Assets/Scripts/Network/Authenticator.cs
Normal file
46
Assets/Scripts/Network/Authenticator.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using PashaBibko.PenguinChase.Extensions;
|
||||
using Unity.Services.Authentication;
|
||||
using Unity.Services.Core;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace PashaBibko.PenguinChase.Network
|
||||
{
|
||||
public static class Authenticator
|
||||
{
|
||||
public static bool IsAuthenticated { get; private set; }
|
||||
private static bool sIsAuthenticating;
|
||||
|
||||
public static IEnumerator Authenticate()
|
||||
{
|
||||
// Early return if already authenticated
|
||||
Debug.Log($"Authenticate called, [Authenticated: {IsAuthenticated}]");
|
||||
if (IsAuthenticated)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Stops multiple authentication attempts at the same time
|
||||
if (sIsAuthenticating)
|
||||
{
|
||||
yield return new WaitUntil(() => IsAuthenticated || !sIsAuthenticating);
|
||||
yield break; // User should be logged in from other attempt
|
||||
}
|
||||
sIsAuthenticating = true;
|
||||
|
||||
Debug.Log("Initializing Unity Services");
|
||||
yield return UnityServices
|
||||
.InitializeAsync()
|
||||
.Await();
|
||||
|
||||
Debug.Log("Signing in anonymously");
|
||||
yield return AuthenticationService.Instance
|
||||
.SignInAnonymouslyAsync()
|
||||
.Await();
|
||||
|
||||
Debug.Log("User has been authenticated");
|
||||
sIsAuthenticating = false;
|
||||
IsAuthenticated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user