当我使用Firebase Unity插件在android中实现FaceBook身份验证功能时,它崩溃了

当我使用Firebase Unity插件在android中实现FaceBook身份验证功能时,它崩溃了,firebase,unity3d,firebase-authentication,Firebase,Unity3d,Firebase Authentication,我正在尝试使用Facebook和Firebase Unity插件在android平台上实现用户身份验证。在我通过facebook登录并希望使用facebook令牌获得firebase凭据后,应用程序崩溃。我可以从androidstudio logcat中找到信息:“A/firebase:firebase身份验证未初始化,无法创建凭据。请先创建身份验证实例。”。Firebase Auth似乎需要一些初始化。但我看到了Firebase Unity文档,找不到任何初始化信息。任何帮助都将不胜感激 我使

我正在尝试使用Facebook和Firebase Unity插件在android平台上实现用户身份验证。在我通过facebook登录并希望使用facebook令牌获得firebase凭据后,应用程序崩溃。我可以从androidstudio logcat中找到信息:“A/firebase:firebase身份验证未初始化,无法创建凭据。请先创建身份验证实例。”。Firebase Auth似乎需要一些初始化。但我看到了Firebase Unity文档,找不到任何初始化信息。任何帮助都将不胜感激

我使用Unity 2018.3.8f1、facebook-Unity-sdk-7.15.1和firebase_Unity_sdk_5.6.0。因为Facebook Unity SDK不能在Unity编辑器中运行,所以我总是通过Unity编辑器构建apk,并在android模拟器中进行尝试

这是我的C#代码(Unity脚本),几乎所有代码都使用firebase文档中的示例代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Facebook.Unity;
using Firebase.Auth;

public class LoginWithFB : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public Firebase.Auth.FirebaseAuth auth;
    void Awake ()
    {
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => 
        {
            var dependencyStatus = task.Result;
            if (dependencyStatus == Firebase.DependencyStatus.Available) 
            {
                // Create and hold a reference to your FirebaseApp,
                // where app is a Firebase.FirebaseApp property of your application class.
                //   app = Firebase.FirebaseApp.DefaultInstance;

                // Set a flag here to indicate whether Firebase is ready to use by your app.
                InitializeFirebase();
            } 
            else 
            {
                UnityEngine.Debug.LogError(System.String.Format("Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                // Firebase Unity SDK is not safe to use here.
            }
        });

        if (!FB.IsInitialized) {
            // Initialize the Facebook SDK
            FB.Init(InitCallback, OnHideUnity);
        } else {
            // Already initialized, signal an app activation App Event
            FB.ActivateApp();
        }
        //auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    }

    protected void InitializeFirebase() 
    {
        //DebugLog("Setting up Firebase Auth");
        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        auth.StateChanged += AuthStateChanged;
        //auth.IdTokenChanged += IdTokenChanged;
        // Specify valid options to construct a secondary authentication object.
        AuthStateChanged(this, null);
    }

    void AuthStateChanged(object sender, System.EventArgs eventArgs) 
    {
        Firebase.Auth.FirebaseAuth senderAuth = sender as Firebase.Auth.FirebaseAuth;
        Firebase.Auth.FirebaseUser user = null;
        //if (senderAuth != null) userByAuth.TryGetValue(senderAuth.App.Name, out user);
        if (senderAuth == auth && senderAuth.CurrentUser != user) 
        {
            bool signedIn = user != senderAuth.CurrentUser && senderAuth.CurrentUser != null;
            if (!signedIn && user != null) 
            {
                Debug.Log("Signed out " + user.UserId);
            }
            user = senderAuth.CurrentUser;
            //userByAuth[senderAuth.App.Name] = user;
            if (signedIn) 
            {
                Debug.Log("Signed in " + user.UserId);
                //displayName = user.DisplayName ?? "";
                //DisplayDetailedUserInfo(user, 1);
            }
        }
    }

    private void InitCallback ()
    {
        if (FB.IsInitialized) {
            // Signal an app activation App Event
            FB.ActivateApp();
            // Continue with Facebook SDK
            // ...
        } else {
            Debug.Log("Failed to Initialize the Facebook SDK");
        }
    }

    private void OnHideUnity (bool isGameShown)
    {
        if (!isGameShown) {
            // Pause the game - we will need to hide
            Time.timeScale = 0;
        } else {
            // Resume the game - we're getting focus again
            Time.timeScale = 1;
        }
    }
    private void AuthCallback (ILoginResult result) 
    {
        if (FB.IsLoggedIn) 
        {
            // AccessToken class will have session details
            var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
            // Print current access token's User ID
            Debug.Log(aToken.UserId);
            Debug.Log(aToken.TokenString);
            Debug.Log(auth);
            // Print current access token's granted permissions
            foreach (string perm in aToken.Permissions) 
            {
                Debug.Log(perm);
            }

            Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential(aToken.TokenString);
            //auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
            auth.SignInWithCredentialAsync(credential).ContinueWith(task => 
            {
                if (task.IsCanceled) 
                {
                    Debug.LogError("SignInWithCredentialAsync was canceled.");
                    return;
                }
                if (task.IsFaulted) 
                {
                    Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
                    return;
                }

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

            Debug.Log("Ready goto AsterPlayMainScene");
            Application.LoadLevel("AsterPlayMainScene");
            //SceneManager.LoadScene("AsterPlayMainScene");

        } 
        else 
        {
            Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential("asadjkahfidhfahfguisodhui");

            Debug.Log("User cancelled login");
            Application.LoadLevel("AsterPlayMainScene");
            //SceneManager.LoadScene("AsterPlayMainScene");
        }
    }

    public void OnFBLoginClick()
    {
        var perms = new List<string>(){"public_profile", "email"};
        FB.LogInWithReadPermissions(perms, AuthCallback);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用Facebook.Unity;
使用Firebase.Auth;
公共类登录WithFB:MonoBehavior
{
//在第一帧更新之前调用Start
void Start()
{
}
//每帧调用一次更新
无效更新()
{
}
public Firebase.Auth.FirebaseAuth Auth;
无效唤醒()
{
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(任务=>
{
var dependencyStatus=task.Result;
if(dependencyStatus==Firebase.dependencyStatus.Available)
{
//创建并保留对FirebaseApp的引用,
//其中app是应用程序类的Firebase.FirebaseApp属性。
//app=Firebase.FirebaseApp.DefaultInstance;
//在此处设置一个标志,指示Firebase是否已准备好供您的应用程序使用。
初始化REBASE();
} 
其他的
{
UnityEngine.Debug.LogError(System.String.Format(“无法解析所有Firebase依赖项:{0}”,dependencyStatus));
//Firebase Unity SDK在此处使用不安全。
}
});
如果(!FB.i初始化){
//初始化Facebook SDK
FB.Init(InitCallback,OnHideUnity);
}否则{
//已初始化,发出应用程序激活应用程序事件的信号
FB.激活EAPP();
}
//auth=Firebase.auth.FirebaseAuth.DefaultInstance;
}
受保护的void initializefrebase()
{
//调试日志(“设置Firebase验证”);
auth=Firebase.auth.FirebaseAuth.DefaultInstance;
auth.StateChanged+=AuthStateChanged;
//auth.IdTokenChanged+=IdTokenChanged;
//指定有效选项以构造辅助身份验证对象。
AuthStateChanged(此,空);
}
void AuthStateChanged(对象发送方,System.EventArgs EventArgs)
{
Firebase.Auth.FirebaseAuth senderAuth=发件人为Firebase.Auth.FirebaseAuth;
Firebase.Auth.FirebaseUser=null;
//if(senderAuth!=null)userByAuth.TryGetValue(senderAuth.App.Name,out user);
if(senderAuth==auth&&senderAuth.CurrentUser!=user)
{
bool signedIn=user!=senderAuth.CurrentUser&&senderAuth.CurrentUser!=null;
如果(!signedIn&&user!=null)
{
Debug.Log(“注销”+user.UserId);
}
user=senderAuth.CurrentUser;
//userByAuth[senderAuth.App.Name]=用户;
如果(签名)
{
Debug.Log(“登录”+user.UserId);
//displayName=user.displayName;
//DisplayDetailedUserInfo(用户,1);
}
}
}
私有void InitCallback()
{
如果(FB.已初始化){
//发出应用程序激活应用程序事件的信号
FB.激活EAPP();
//继续使用Facebook SDK
// ...
}否则{
Log(“未能初始化Facebook SDK”);
}
}
私密的虚空隐藏(如图所示)
{
如果(!isgame显示){
//暂停游戏-我们需要隐藏
Time.timeScale=0;
}否则{
//继续比赛-我们再次获得焦点
Time.timeScale=1;
}
}
私有void AuthCallback(ILoginResult结果)
{
如果(FB.IsLoggedIn)
{
//AccessToken类将具有会话详细信息
var aToken=Facebook.Unity.AccessToken.CurrentAccessToken;
//打印当前访问令牌的用户ID
Debug.Log(aToken.UserId);
Log(aToken.TokenString);
Debug.Log(auth);
//打印当前访问令牌授予的权限
foreach(aToken.Permissions中的字符串perm)
{
调试日志(perm);
}
Firebase.Auth.Credential Credential=Firebase.Auth.FacebookAuthProvider.GetCredential(aToken.TokenString);
//auth=Firebase.auth.FirebaseAuth.DefaultInstance;
身份验证签名与凭证同步(凭证)。继续(任务=>
{
如果(task.IsCanceled)
{
LogError(“SignInWithCredentialAsync已取消”);
返回;
}
if(task.IsFaulted)
{
LogError(“SignInWithCredentialAsync遇到错误:“+task.Exception”);
返回;
}
Firebase.Auth.FirebaseUser newUser=task.Result;
LogFormat(“用户成功登录:{0}({1})”,
newUser.DisplayName,newUser.UserId);
});
Log(“准备好转到主场景”);
Application.LoadLevel(“AsterPlayMainScene”
2019-03-31 22:22:25.313 20155-20186/? E/firebase: g_methods_cached
2019-03-31 22:22:25.313 20155-20186/? A/firebase: Firebase Auth was not initialized, unable to create a Credential. Create an Auth instance first.

Also I can find callstack from logcat:
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #32  il 00000012  at (wrapper managed-to-native) Firebase.Auth.AuthUtilPINVOKE.FacebookAuthProvider_GetCredential (string) <0x00012>
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #33  il 00000013  at Firebase.Auth.FacebookAuthProvider.GetCredential (string) [0x00000] in <7ff3b01c54e9444eb36c9f4350522434>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #34  il 000000eb  at LoginWithFB.AuthCallback (Facebook.Unity.ILoginResult) [0x00064] in <9cb866e367a54ac3882a6e4448a28c2e>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #35  il 00000069  at Facebook.Unity.CallbackManager.TryCallCallback<Facebook.Unity.ILoginResult> (object,Facebook.Unity.IResult) [0x0000a] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #36  il 000000f3  at Facebook.Unity.CallbackManager.CallCallback (object,Facebook.Unity.IResult) [0x00046] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #37  il 00000077  at Facebook.Unity.CallbackManager.OnFacebookResponse (Facebook.Unity.IInternalResult) [0x00021] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #38  il 00000033  at Facebook.Unity.FacebookBase.OnAuthResponse (Facebook.Unity.LoginResult) [0x00019] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #39  il 0000003c  at Facebook.Unity.Mobile.MobileFacebook.OnLoginComplete (Facebook.Unity.ResultContainer) [0x00007] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.549 20155-20186/? E/CRASH:  #40  il 00000046  at Facebook.Unity.FacebookGameObject.OnLoginComplete (string) [0x0000c] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.549 20155-20186/? E/CRASH:  #41  il 0000005b  at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object (object,intptr,intptr,intptr) <0x0005b>