|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using Google;
|
|
|
|
|
using PlayFab;
|
|
|
|
|
using PlayFab.ClientModels;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
public class GoogleSignInManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
//#if UNITY_IOS
|
|
|
|
|
// private string webClientId = "723833850517-2j378fbm2a8u644p4qqod4qbi93dsgom.apps.googleusercontent.com"; // Replace with your actual Google Web Client ID
|
|
|
|
|
//#elif UNITY_ANDROID
|
|
|
|
|
// private string webClientId = "723833850517-tfaer77vetml1bhv9qbj3o84ec52u45d.apps.googleusercontent.com"; // Replace with your actual Google Web Client ID
|
|
|
|
|
//#endif
|
|
|
|
|
private string webClientId = "723833850517-865419enf8t0j1itln3cgmd1b67shsue.apps.googleusercontent.com";
|
|
|
|
|
|
|
|
|
|
private GoogleSignInConfiguration configuration;
|
|
|
|
|
public Bootstrapper bootstrapper; // Assign Bootstrapper in Inspector
|
|
|
|
|
public Button googleSignInButton;
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
configuration = new GoogleSignInConfiguration
|
|
|
|
|
{
|
|
|
|
|
WebClientId = webClientId,
|
|
|
|
|
RequestIdToken = true,
|
|
|
|
|
RequestAuthCode = true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (PlayerPrefs.HasKey("PlayFabID"))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("User already signed in, attempting silent login...");
|
|
|
|
|
SignInSilently();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("No saved PlayFab ID, waiting for user sign-in.");
|
|
|
|
|
SignInWithGoogle();
|
|
|
|
|
googleSignInButton.gameObject.SetActive(true);
|
|
|
|
|
googleSignInButton.onClick.AddListener(SignInWithGoogle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SignInWithGoogle()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Starting Google Sign-In...");
|
|
|
|
|
GoogleSignIn.Configuration = configuration;
|
|
|
|
|
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnGoogleSignIn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnGoogleSignIn(Task<GoogleSignInUser> task)
|
|
|
|
|
{
|
|
|
|
|
if (task.IsFaulted)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("Google Sign-In Failed: " + task.Exception);
|
|
|
|
|
}
|
|
|
|
|
else if (task.IsCanceled)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Google Sign-In Canceled.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Google Sign-In Successful! Fetching PlayFab login...");
|
|
|
|
|
|
|
|
|
|
string idToken = task.Result.IdToken;
|
|
|
|
|
PlayerPrefs.SetString("GoogleIdToken", idToken); // Save Google Token for auto-login
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
|
|
LoginToPlayFab(idToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoginToPlayFab(string idToken)
|
|
|
|
|
{
|
|
|
|
|
var request = new LoginWithGoogleAccountRequest
|
|
|
|
|
{
|
|
|
|
|
TitleId = PlayFabSettings.TitleId,
|
|
|
|
|
ServerAuthCode = idToken,
|
|
|
|
|
CreateAccount = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PlayFabClientAPI.LoginWithGoogleAccount(request, OnPlayFabLoginSuccess, OnPlayFabLoginFailure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPlayFabLoginSuccess(LoginResult result)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("PlayFab Login Success! PlayFab ID: " + result.PlayFabId);
|
|
|
|
|
|
|
|
|
|
// Save PlayFab ID to keep the user signed in automatically
|
|
|
|
|
PlayerPrefs.SetString("PlayFabID", result.PlayFabId);
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
|
|
// Start the game after login
|
|
|
|
|
bootstrapper.StartGame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPlayFabLoginFailure(PlayFabError error)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("PlayFab Login Failed: " + error.GenerateErrorReport());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SignInSilently()
|
|
|
|
|
{
|
|
|
|
|
if (PlayerPrefs.HasKey("GoogleIdToken"))
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Attempting silent Google Sign-In...");
|
|
|
|
|
string savedIdToken = PlayerPrefs.GetString("GoogleIdToken");
|
|
|
|
|
LoginToPlayFab(savedIdToken); // Skip Google UI, go directly to PlayFab login
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("No Google ID Token saved, user must sign in manually.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SignOut()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Signing out user...");
|
|
|
|
|
|
|
|
|
|
// Clear stored login data
|
|
|
|
|
PlayerPrefs.DeleteKey("PlayFabID");
|
|
|
|
|
PlayerPrefs.DeleteKey("GoogleIdToken");
|
|
|
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
|
|
// Sign out from Google
|
|
|
|
|
GoogleSignIn.DefaultInstance.SignOut();
|
|
|
|
|
|
|
|
|
|
Debug.Log("User signed out. They will need to log in again.");
|
|
|
|
|
}
|
|
|
|
|
}
|