this is the code i am using, idk about session keys and other concepts, chat gpt has given me the player prefs to store the access tokens, but as i am trying to use the cloud save, its not working, and in the first place when i am openning the game for the first time, and siggning in, then the cloud save is woking perfectlty, but as the player reopens the game, its not working, because ofc it needed to be sign in again, how can i do that, the code below, you can attatch it to a button, taken from the ui sample provided by the authentication, Help me,
using System;
using System.Text;
using Unity.Services.Core;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Unity.Services.Authentication;
using Unity.Services.CloudSave;
namespace Unity.Services.Authentication.PlayerAccounts.Samples
{
class PlayerAccountsDemo : MonoBehaviour
{
[SerializeField]
Text m_StatusText;
[SerializeField]
Text m_ExceptionText;
string m_ExternalIds;
async void Start()
{
await UnityServices.InitializeAsync();
PlayerAccountService.Instance.SignedIn += OnSignedIn;
// Check if the player is already signed in
if (PlayerPrefs.HasKey("PlayerSignedIn") && PlayerPrefs.GetInt("PlayerSignedIn") == 1)
{
if (PlayerPrefs.HasKey("AccessToken"))
{
string accessToken = PlayerPrefs.GetString("AccessToken"); // Get stored access token
Debug.Log("---------- Retrieved AccessToken: " + accessToken);
if (!string.IsNullOrEmpty(accessToken))
{
SignInWithUnity(accessToken);
}
}
}
}
public async void StartSignInAsync()
{
if (PlayerAccountService.Instance.IsSignedIn)
{
SignInWithUnity(PlayerAccountService.Instance.AccessToken);
return;
}
try
{
await PlayerAccountService.Instance.StartSignInAsync();
PlayerPrefs.SetString("AccessToken", PlayerAccountService.Instance.AccessToken); // Store access token
PlayerPrefs.Save(); // Save changes
PlayerPrefs.SetInt("PlayerSignedIn", 1); // Store sign-in state
Debug.Log("---------- Stored AccessToken: " + PlayerAccountService.Instance.AccessToken);
UpdateUI();
}
catch (RequestFailedException ex)
{
Debug.LogException(ex);
SetException(ex);
}
}
public void SignOut()
{
AuthenticationService.Instance.SignOut();
PlayerAccountService.Instance.SignOut();
PlayerPrefs.DeleteKey("PlayerSignedIn"); // Remove sign-in state
PlayerPrefs.DeleteKey("AccessToken"); // Remove access token
UpdateUI();
m_StatusText.text = "You have logged out.";
}
async void SignInWithUnity(string accessToken)
{
try
{
await AuthenticationService.Instance.SignInWithUnityAsync(accessToken);
m_ExternalIds = GetExternalIds(AuthenticationService.Instance.PlayerInfo);
UpdateUI();
SceneManager.LoadScene("HomeScreenScene"); // Add this line
}
catch (RequestFailedException ex)
{
Debug.LogException(ex);
SetException(ex);
}
}
void OnSignedIn()
{
SignInWithUnity(PlayerAccountService.Instance.AccessToken);
}
void UpdateUI()
{
var statusBuilder = new StringBuilder();
statusBuilder.AppendLine($"Player Accounts State: <b>{(PlayerAccountService.Instance.IsSignedIn ? "Signed in" : "Signed out")}</b>");
statusBuilder.AppendLine($"Player Accounts Access token: <b>{(string.IsNullOrEmpty(PlayerAccountService.Instance.AccessToken) ? "Missing" : "Exists")}</b>\n");
statusBuilder.AppendLine($"Authentication Service State: <b>{(AuthenticationService.Instance.IsSignedIn ? "Signed in" : "Signed out")}</b>");
if (AuthenticationService.Instance.IsSignedIn)
{
statusBuilder.AppendLine(GetPlayerInfoText());
statusBuilder.AppendLine($"PlayerId: <b>{AuthenticationService.Instance.PlayerId}</b>");
}
m_StatusText.text = statusBuilder.ToString();
SetException(null);
}
string GetExternalIds(PlayerInfo playerInfo)
{
if (playerInfo.Identities == null)
{
return "None";
}
var sb = new StringBuilder();
foreach (var id in playerInfo.Identities)
{
sb.Append(" " + id.TypeId);
}
return sb.ToString();
}
string GetPlayerInfoText()
{
return $"ExternalIds: <b>{m_ExternalIds}</b>";
}
void SetException(Exception ex)
{
m_ExceptionText.text = ex?.ToString() ?? string.Empty;
}
}
}