89 lines
2.9 KiB
C#
89 lines
2.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Firebase;
|
|
using Firebase.Auth;
|
|
using Google;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class GoogleLoginManager : MonoBehaviour {
|
|
public string webClientId = "871331112347-3qpmns59teudhm5qugl4kiau6eo1efm2.apps.googleusercontent.com";
|
|
private FirebaseAuth auth;
|
|
private GoogleSignInConfiguration config;
|
|
public NewLoginManager loginManager;
|
|
public AccountSettings accountSettings;
|
|
|
|
void Awake(){
|
|
config = new GoogleSignInConfiguration{WebClientId=webClientId, RequestEmail=true, RequestIdToken=true};
|
|
CheckFirebaseDependencies();
|
|
}
|
|
|
|
void CheckFirebaseDependencies(){
|
|
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task=>{
|
|
if(task.IsCompleted){
|
|
if(task.Result == DependencyStatus.Available){
|
|
auth = FirebaseAuth.DefaultInstance;
|
|
}else{
|
|
Debug.Log("googlesign:Firebase Deps missing, " + task.Result.ToString());
|
|
}
|
|
}else{
|
|
Debug.Log("googlesign:Dependency test not completed " + task.Exception.Message);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void SignInWithGoogle(){OnSignIn();}
|
|
public void SignOutFromGoogle(){OnSignOut();}
|
|
|
|
private void OnSignIn(){
|
|
GoogleSignIn.Configuration = config;
|
|
GoogleSignIn.Configuration.UseGameSignIn = false;
|
|
GoogleSignIn.Configuration.RequestIdToken = true;
|
|
Debug.Log("googlesign:Call Signin");
|
|
|
|
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(AuthDone);
|
|
}
|
|
private void OnSignOut(){
|
|
GoogleSignIn.DefaultInstance.SignOut();
|
|
}
|
|
public void OnDisconnect(){
|
|
GoogleSignIn.DefaultInstance.Disconnect();
|
|
}
|
|
|
|
public static string loggedEmail;
|
|
void AuthDone(Task<GoogleSignInUser> task){
|
|
if(task.IsFaulted){
|
|
using(IEnumerator<Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator()){
|
|
if(enumerator.MoveNext()){
|
|
GoogleSignIn.SignInException error = (GoogleSignIn.SignInException)enumerator.Current;
|
|
Debug.Log("googlesign:Got error: " + error.Status + " " + error.Message);
|
|
}else{
|
|
Debug.Log("googlesign:Unexpected Exception : " + task.Exception);
|
|
}
|
|
}
|
|
}else if(task.IsCanceled){
|
|
Debug.Log("googlesign:User aborted");
|
|
}else{
|
|
Debug.Log("googlesign:User logged");
|
|
Debug.Log($"user={task.Result.DisplayName}, email={task.Result.Email}");
|
|
loggedEmail = task.Result.Email;
|
|
StartCoroutine(OnSignDone(task.Result.DisplayName));
|
|
|
|
}
|
|
}
|
|
|
|
IEnumerator OnSignDone(string displayName){
|
|
PlayerPrefs.SetString("displayname", displayName);
|
|
PlayerPrefs.Save();
|
|
yield return new WaitForSeconds(0.2f);
|
|
|
|
if(loginManager != null){loginManager.OnGoogleSignComplete();}
|
|
|
|
if(accountSettings != null){
|
|
accountSettings.GoogleSignComplete();
|
|
}
|
|
}
|
|
}
|