Home > other >  The function inside ContinueWith() is not working
The function inside ContinueWith() is not working

Time:11-12

public void Login()
{
    string email = emailInputField.text;
    string password = passwordInputField.text;
    auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(
        task =>
        {
            if (task.IsCompleted && !task.IsCanceled && !task.IsFaulted)
            {
                Debug.Log("[Login Success] ID: "   auth.CurrentUser.UserId);
                PlayerInformation.auth = auth;
                SceneManager.LoadScene("SongSelectScene");
            }
            else
            {
                messageUI.text = "[Login Failed] Please check your account";
                Debug.Log("[Login Failed] Please check your account");
            }
        });
}

I'm a student learning unity.. I've trying to make a sign-up and log-in function in unity by firebase. (using unity 2020.3.20f1)

However, except Debug.Log() function, it doesn't work when I execute it in unity. For the above code, Debug.Log() is working when I input valid email and password but SceneManager.LoadScene() is not working...

I tried to change the value(like messageUI.text = ~~) in the ContinueWith() but it is not working either... I really need your help... Thank you.

CodePudding user response:

Most of the Unity API can only be used on the Unity main thread.

ContinueWith is not guarenteed to be executed on the same thread as the task was started on!

Specifically for this reason Firebase offers the TaskExtensions ConinueWithOnMainThread which in the background uses something similar to e.g. UnityMainThreadDispatcher for dispatching the result callbacks back into the Unity main thread via an internal thread-save ConcurrentQueue and a dedicated Update method working it off.

public void Login()
{
    string email = emailInputField.text;
    string password = passwordInputField.text;
    auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWithOnMainThread(
        task =>
        {
            if (task.IsCompleted && !task.IsCanceled && !task.IsFaulted)
            {
                Debug.Log("[Login Success] ID: "   auth.CurrentUser.UserId);
                PlayerInformation.auth = auth;
                SceneManager.LoadScene("SongSelectScene");
            }
            else
            {
                messageUI.text = "[Login Failed] Please check your account";
                Debug.Log("[Login Failed] Please check your account");
            }
        });
}
  • Related