I have tried Google Play Gaming Services, Firebase Authentication, Facebook login, and the last one and latest one, Unity player accounts, every time its just ended up with Cloud Save exception: access token is missing unity, but when I am trying to do anonymous sign-in its just working perfectly, right now I wanted to use unity player accounts.
Here are my two scripts:
LoginController.cs
using System;
using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Authentication.PlayerAccounts;
using Unity.Services.Core;
using UnityEngine;
public class LoginController : MonoBehaviour
{
public event EventHandler<SignedInEventArgs> OnSignedIn;
private void Awake()
{
UnityServices.InitializeAsync(); // Remove .Forget() if not using UniTask
PlayerAccountService.Instance.SignedIn += HandleSignedIn;
}
private async void HandleSignedIn()
{
Debug.Log("----------Token script is working right now");
try
{
Debug.Log("----------its trying to access the token");
var accessToken = PlayerAccountService.Instance.AccessToken;
await SignInWithUnityAsync(accessToken);
}
catch (Exception ex)
{
Debug.LogError($"Exception during sign-in: {ex.Message}");
}
}
public async Task InitSignIn()
{
await PlayerAccountService.Instance.StartSignInAsync();
}
private async Task SignInWithUnityAsync(string accessToken)
{
try
{
await AuthenticationService.Instance.SignInWithUnityAsync(accessToken);
Debug.Log("SignIn is successful.");
var playerInfo = AuthenticationService.Instance.PlayerInfo;
var playerName = await AuthenticationService.Instance.GetPlayerNameAsync();
// Log the player ID
Debug.Log($"Player ID: {playerInfo.Id}");
OnSignedIn?.Invoke(this, new SignedInEventArgs(playerInfo, playerName));
}
catch (AuthenticationException ex)
{
Debug.LogError($"Authentication Exception: {ex.Message}");
}
catch (RequestFailedException ex)
{
Debug.LogError($"Request Failed Exception: {ex.Message}");
}
}
private void OnDestroy()
{
PlayerAccountService.Instance.SignedIn -= HandleSignedIn;
}
}
public class SignedInEventArgs : EventArgs
{
public PlayerInfo PlayerInfo { get; }
public string PlayerName { get; }
public SignedInEventArgs(PlayerInfo playerInfo, string playerName)
{
PlayerInfo = playerInfo;
PlayerName = playerName;
}
}
UILogin.cs
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using System.Threading.Tasks;
public class UILogin : MonoBehaviour
{
[SerializeField] private Button loginButton;
[SerializeField] private TMP_Text userIdText;
[SerializeField] private Transform loginPanel;
[SerializeField] private Transform userPanel;
[SerializeField] private LoginController loginController;
private void OnEnable()
{
loginButton.onClick.AddListener(LoginButtonPressed);
loginController.OnSignedIn += HandleSignedIn;
}
private void HandleSignedIn(object sender, SignedInEventArgs args)
{
loginPanel.gameObject.SetActive(false);
userPanel.gameObject.SetActive(true);
userIdText.text = $"id_{args.PlayerInfo.Id}";
Debug.Log($"Player ID: {args.PlayerInfo.Id}, Player Name: {args.PlayerName}");
}
private void LoginButtonPressed()
{
loginController.InitSignIn().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.LogError($"Error during login: {task.Exception}");
}
});
}
private void OnDisable()
{
loginButton.onClick.RemoveListener(LoginButtonPressed);
loginController.OnSignedIn -= HandleSignedIn;
}
}
I have done every steps shown by many youtubers, this is the issue none has answered. Please help.