AuthenticationManager

 avatar
SajStudios
csharp
18 days ago
1.6 kB
4
Indexable
using System.Collections.Generic;
using Unity.Services.Authentication;
using Unity.Services.CloudSave;
using Unity.Services.Core;
using UnityEngine;

public class AuthenticationManager : MonoBehaviour
{
    private async void Awake()
    {
        await UnityServices.InitializeAsync();
        AuthenticationServiceEvents();
        await AuthenticationService.Instance.SignInAnonymouslyAsync();
    }

    private void AuthenticationServiceEvents()
    {
        AuthenticationService.Instance.SignedIn += async () =>
        {
            print(3);
            var _playerCloudData = await CloudSaveService.Instance.Data.Player.LoadAsync(new HashSet<string>
            {
                "playerID",
            });

            if (_playerCloudData.ContainsKey("playerID"))
            {
                CloudSaveManager.Instance.LoadData();
            }
            else
            {
                try
                {
                    CloudSaveManager.Instance.SaveData();
                }
                catch (AuthenticationException err)
                {
                    Debug.Log(err);
                }
            }
        };

        AuthenticationService.Instance.SignedOut += () =>
        {
            print(AuthenticationService.Instance.PlayerName + ". Player Signed Out.");
        };

        AuthenticationService.Instance.Expired += () =>
        {
            print("Player Session Expired.");
        };

        AuthenticationService.Instance.SignInFailed += (err) =>
        {
            print(err.Message);
        };
    }
}
Leave a Comment