C# Firebase Google登录任务未在Unity中运行ContinueWith()方法

C# Firebase Google登录任务未在Unity中运行ContinueWith()方法,c#,firebase,unity3d,task,google-signin,C#,Firebase,Unity3d,Task,Google Signin,我将FirebaseAuth.unitypackage和google-signin-plugin-0.1.4.unitypackage导入到我的项目中,并作为signin manager编写了以下代码: using System; using System.Threading.Tasks; using Firebase.Auth; using Google; using UnityEngine; public class GoogleSignInManager : MonoBehaviour

我将FirebaseAuth.unitypackage和google-signin-plugin-0.1.4.unitypackage导入到我的项目中,并作为signin manager编写了以下代码:

using System;
using System.Threading.Tasks;
using Firebase.Auth;
using Google;
using UnityEngine;

public class GoogleSignInManager : MonoBehaviour
{
    public static GoogleSignInManager Instance;
    public GoogleSignInManager() => Instance = this;

    private FirebaseAuth _auth;
    public FirebaseUser CurrentUser => _auth?.CurrentUser;

    private void Awake()
    {
        _auth = FirebaseAuth.DefaultInstance;
        GoogleSignIn.Configuration = new GoogleSignInConfiguration
        {
            RequestIdToken = true,
            WebClientId = "xxxx"
        };
    }

    public Task<FirebaseUser> SignIn()
    {
        var signIn = GoogleSignIn.DefaultInstance.SignIn();
        var signInCompleted = new TaskCompletionSource<FirebaseUser>();
        signIn.ContinueWith(task =>
        {
            Debug.Log($"In signIn ContinueWith (status = {task.Status:g})");
            if (task.IsCanceled)
                signInCompleted.SetCanceled();
            else if (task.IsFaulted)
                signInCompleted.SetException(task.Exception ?? new Exception("Task Is Faulted"));
            else
            {
                var signInUser = task.Result;
                var credential = GoogleAuthProvider.GetCredential(signInUser.IdToken, null);
                _auth.SignInWithCredentialAsync(credential).ContinueWith(authTask =>
                {
                    Debug.Log($"In SignInWithCredentialAsync ContinueWith (status = {authTask.Status:g})");
                    if (authTask.IsCanceled)
                        signInCompleted.SetCanceled();
                    else if (authTask.IsFaulted)
                        signInCompleted.SetException(authTask.Exception ?? new Exception("Task Is Faulted"));
                    else
                        signInCompleted.SetResult(authTask.Result);
                });
            }
        });
        return signInCompleted.Task;
    }

    public void SignOut()
    {
        _auth.SignOut();
        GoogleSignIn.DefaultInstance.SignOut();
    }
}
Debug.Loguser.Email;或者任何其他主线程方法都不会运行。我想在UnityEngine.UI.Text中显示用户的电子邮件地址,但它不会被调用。

ContinueWith不保证在Unity主线程上执行

因为您有特定于Unity的Firebase,所以使用


健全性检查:你是在发布模式下构建的吗?是的,我是@HarshdeepSinghDebug。Log@HarshdeepSingh我使用IngameDebugConsole包在运行时查看调试,我也在没有这些行的情况下进行了测试,不走运。尝试使用logcat查看登录插件是否记录了任何错误。您提到的代码不起作用,但当我在调用类中添加一个函数并在使用Debug.Loguser.Email;的地方调用它时;,成功了!
GoogleSignInManager.Instance.SignIn().ContinueWith(task =>
{
    if (task.IsCanceled)
        Debug.Log("Please Try Again!");
    else if (task.IsFaulted)
        Debug.LogError(task.Exception?.Message);
    else
    {
        var user = task.Result;
        Debug.Log(user.Email);
        email.text = user.Email;
    }
});
GoogleSignInManager.Instance.SignIn().ContinueWithOnMainThread(task =>
{
    if (task.IsCanceled)
        Debug.Log("Please Try Again!");
    else if (task.IsFaulted)
        Debug.LogError(task.Exception?.Message);
    else
    {
        var user = task.Result;
        Debug.Log(user.Email);
    }
});