有些代码不起作用。我不知道为什么。如何使代码有效?

时间:2019-07-24 04:33:27

标签: firebase unity3d authentication methods

我想要统一登录屏幕。我将使用Firebase,并按照Firebase页面和一些youtube频道上的手册学习如何使用Firebase。

和..某些代码不起作用。我使用了Firebase给出的代码,但登录成功后的代码不起作用。嗯..对不起,我英语不好。请参阅代码。谢谢。

此代码无效

authUI.ShowLoggedInPanel();// 로그인 성공 시 메인메뉴로 전환!
authUI.LoggedInUserEmail.text = newUser.Email;

我不知道我可以尝试什么。

private void TryLoginWithFirebaseAuth(string email, string password) // 기존 사용자 로그인
{
    auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => 
    {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
            return;
        }

        // 로그인 성공 (Maybe Login success?)

        Firebase.Auth.FirebaseUser newUser = task.Result;
        Debug.LogFormat("User signed in successfully: {0} ({1})",
            newUser.DisplayName, newUser.UserId);

        authUI.ShowLoggedInPanel();// 로그인 성공 시 메인메뉴로 전환!
        authUI.LoggedInUserEmail.text = newUser.Email;
    });
}

它没有向我显示错误。但只是..它不起作用。 有人可以帮我吗?

3 个答案:

答案 0 :(得分:0)

我对Firebase不太了解,但问题可能出在线程上。

所有(最多)Unity API调用必须在主线程中完成。由于Firebase在后台线程中异步执行其填充内容,因此某些调用可能只会失败。

您应该使用Action参数,以便将回调传递给类似这样的方法

private void TryLoginWithFirebaseAuth(string email, string password, Action<Firebase.Auth.FirebaseUser> successCallback) // 기존 사용자 로그인
{
    auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => 
    {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
            return;
        }

        // 로그인 성공 (Maybe Login success?)

        Firebase.Auth.FirebaseUser newUser = task.Result;

        successCallback?Invoke(newUser);
    });
}

并像

一样使用它
TryLoginWithFirebaseAuth(someName, somePassword, onSuccess =>
    {
        Debug.LogFormat("User signed in successfully: {0} ({1})",
            onSuccess.DisplayName, onSuccess.UserId);

        authUI.ShowLoggedInPanel();// 로그인 성공 시 메인메뉴로 전환!
        authUI.LoggedInUserEmail.text = onSuccess.Email;
    }

这确保了它肯定在Unity主线程中执行。

答案 1 :(得分:0)

如果没有更多上下文信息,很难肯定地回答您的问题(例如,Unity控制台中是否有有用的日志?)。

一些注意事项: 您必须确保已在Firebase控制台中设置了身份验证。这意味着单击侧栏中的“身份验证”并显式启用所需的方法: Enable Authentication

类似地,您需要确保正确设置了应用程序。您的套件ID /捆绑ID(Android / iOS)需要与您控制台中的ID匹配。同样,您需要上传签名证书的SHA1才能使其在Android中运行。

我假设您现在只是调试,因此可以在这里获取调试证书:https://developers.google.com/android/guides/client-auth

您将要打开项目设置:project settings

然后在此处添加指纹: add fingerprint

按照@derHugo的建议,除了上面的基本设置说明之外,线程化可能是一个问题。我最近在这里写了一篇文章,介绍如何在Unity中使用线程和Firebase:https://firebase.googleblog.com/2019/07/firebase-and-tasks-how-to-deal-with.html

使代码线程安全的最简单方法是将ContinueWith替换为ContinueWithOnMainThread。这是Firebase插件提供的Task的扩展方法,可以更轻松地使用。

我希望所有的帮助!

答案 2 :(得分:0)

这是Unity中使用Firebase的经典任务延续问题。使用ContinueWith时,不能确保在Unity主线程上调用它。您要authUI.ShowLoggedInPanel();authUI.LoggedInUserEmail.text = newUser.Email;进行的操作需要在Unity主线程上执行。如果您尝试访问ContinueWith内部的GameObjects,则将失败。代码只是隐藏了出来,在控制台中没有任何错误。

解决方案: 正是出于这个原因而使用ContinueWithOnMainThread中的Firebase extensions,而不是ContinueWith