Unity версия: 2020.3.25f1
Firebase версия: 9.0.0
Google Sign In Plugin версия: 1.0.4
Взял этот скрипт с просторов интернета, для понимания происходящего.
using Firebase.Auth;
using Firebase;
using Google;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System.Threading.Tasks;
using System.Collections;
using System;
using Firebase.Extensions;
public class Login : MonoBehaviour
{
[TextArea] public string GoogleWebApi = "Web Key Firebase";
private GoogleSignInConfiguration config;
Firebase.DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;
FirebaseAuth auth;
FirebaseUser user;
[SerializeField] private TextMeshProUGUI status;
private void Awake()
{
config = new Google.GoogleSignInConfiguration
{
WebClientId = GoogleWebApi,
RequestIdToken = true
};
}
private void Start()
{
InitFirebase();
}
private void InitFirebase()
{
auth = FirebaseAuth.DefaultInstance;
}
public void GoogleSignInClick()
{
status.text = $"Вызываем лог..";
GoogleSignIn.Configuration = config;
GoogleSignIn.Configuration.UseGameSignIn = false;
GoogleSignIn.Configuration.RequestIdToken = true;
GoogleSignIn.Configuration.RequestEmail = true;
GoogleSignIn.DefaultInstance.SignIn().ContinueWithOnMainThread(SignWithGoogleFinished);
}
private void SignWithGoogleFinished(Task<GoogleSignInUser> task)
{
Task<GoogleSignInUser> signIn = GoogleSignIn.DefaultInstance.SignIn();
TaskCompletionSource<FirebaseUser> signInCompleted = new TaskCompletionSource<FirebaseUser>();
signIn.ContinueWithOnMainThread(task => {
if (task.IsCanceled)
{
signInCompleted.SetCanceled();
status.text = "юзер передумал";
}
else if (task.IsFaulted)
{
signInCompleted.SetException(task.Exception);
StartCoroutine(OnFailure(task.Exception));
}
else
{
Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(((Task<GoogleSignInUser>)task).Result.IdToken, null);
auth.SignInWithCredentialAsync(credential).ContinueWithOnMainThread(authTask => {
if (authTask.IsCanceled)
{
signInCompleted.SetCanceled();
status.text = "юзер передумал";
}
else if (authTask.IsFaulted)
{
signInCompleted.SetException(authTask.Exception);
StartCoroutine(OnFailure(authTask.Exception));
}
else
{
signInCompleted.SetResult(((Task<FirebaseUser>)authTask).Result);
StartCoroutine(OnSuccess(authTask.Result));
}
});
}
});
}
private IEnumerator OnSuccess(FirebaseUser authTaskResult)
{
yield return new WaitForEndOfFrame();
Debug.Log("OnSuccess");
status.text = $"Всё гуд, твой ник {user.DisplayName}, почта {user.Email}";
}
private IEnumerator OnFailure(AggregateException authTaskException)
{
yield return new WaitForEndOfFrame();
status.text = $"{authTaskException}";
}
}
Сбилдил проект, захожу в игру. Там есть кнопка Sign In, нажимая на нее показывается панелька с выбором аккаунта, при выборе аккаунта приложение просто крашится, что делать?
Уточнение: билд под андроид!