如何在 Unity 中调用 Firebase 电子邮件登录任务中的函数?

How to call a function inside Firebase email login Task in Unity?

在我的 unity 应用程序中,在使用电子邮件和密码登录时,我使用的是 firebase 登录。我的问题是当我尝试在任务中调用函数或执行组代码时没有任何反应。甚至没有错误。如何调用里面的函数。

public void signin()
{
    auth.SignInWithEmailAndPasswordAsync(email.text, password.text).ContinueWith(task => {
         if (task.IsCanceled) 
         {
            Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
            return;
        }
         if (task.IsFaulted)
         {
        Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
        return;
        }

        Firebase.Auth.FirebaseUser newUser = task.Result;

        //========Checking Email Verified Or Not======

        if(auth.CurrentUser.IsEmailVerified== true)
        {
            Debug.LogFormat("User signed in successfully: {0} ({1})",newUser.DisplayName, newUser.UserId);

         //=================Here is the Problem this is not calling==========
           MyFunction();
         //==============================================
        }
        else
        {
        print("Email Not Verified");
        auth.SignOut(); 
        }
        //===========
       
        });     
}

一般来说,这类事情最常见的问题是大多数 Unity API 只能在主线程上使用。

Task.ContinueWith不保证在主线程中执行,但可能发生在任何异步后台thread/task

因此 Firebase 提供了一个扩展方法 ContinueWithOnMainThread 你应该改用它,它确保内部代码块在 Unity 主线程上执行。

所以只需更换

auth.SignInWithEmailAndPasswordAsync(email.text, password.text).ContinueWith(task => {

auth.SignInWithEmailAndPasswordAsync(email.text, password.text).ContinueWithOnMainThread(task => {

确保文件顶部有 using Firebase.Extensions