在一个项目中使用数据库和auth包时,Unity Firebase [到Android]出错

时间:2016-12-01 15:12:00

标签: android unity3d firebase firebase-realtime-database firebase-authentication

我正在Unity3d中开发一款使用Firebase身份验证的Android应用程序,在我尝试使用Firebase数据库包之前,它运行良好。它进口得很好。但是当我输入代码来为我的应用程序的Start()函数设置我的数据库时,例如FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-Firebase-APP.Firebaseio.com/");(在Firebase统一指南https://Firebase.google.com/docs/database/unity/start中)。几乎每次我使用身份验证或数据库功能时,或者几秒钟之后崩溃都会崩溃。

在添加数据库包之前,这是我的start()函数:

Firebase.Auth.FirebaseAuth auth;
Firebase.Auth.FirebaseUser user;
Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;

    void Start () {
        dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
        if (dependencyStatus != Firebase.DependencyStatus.Available) {
            Firebase.FirebaseApp.FixDependenciesAsync().ContinueWith(task => {
                dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
                if (dependencyStatus == Firebase.DependencyStatus.Available) {
                    InitializeFirebase();
                } else {
                    // This should never happen if we're only using Firebase Analytics.
                    // It does not rely on any external dependencies.
                    Debug.LogError(
                        "Could not resolve all Firebase dependencies: " + dependencyStatus);
                }
            });
        } else {
            InitializeFirebase();
        }
}void InitializeFirebase() {
        Debug.Log("Setting up Firebase Auth");
        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        auth.StateChanged += AuthStateChanged;
    }
    // Track state changes of the auth object.
    void AuthStateChanged(object sender, System.EventArgs eventArgs) {
        if (auth.CurrentUser != user) {
            if (user == null && auth.CurrentUser != null) {
                Debug.Log("Signed in " + auth.CurrentUser.DisplayName);
            } else if (user != null && auth.CurrentUser == null) {
                Debug.Log("Signed out " + user.DisplayName);
            }
            user = auth.CurrentUser;
            //Debug.Log("Signed in " + auth.CurrentUser.DisplayName);

        }
    }

这是我应用数据库连接后的代码(我刚刚在InitializeFirebase()函数的初始化和几行中添加了一个变量):

Firebase.Auth.FirebaseAuth auth;
Firebase.Auth.FirebaseUser user;
DatabaseReference mRef;
Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;

    void Start () {


        dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
        if (dependencyStatus != Firebase.DependencyStatus.Available) {
            Firebase.FirebaseApp.FixDependenciesAsync().ContinueWith(task => {
                dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
                if (dependencyStatus == Firebase.DependencyStatus.Available) {
                    InitializeFirebase();
                } else {
                    // This should never happen if we're only using Firebase Analytics.
                    // It does not rely on any external dependencies.
                    Debug.LogError(
                        "Could not resolve all Firebase dependencies: " + dependencyStatus);
                }
            });
        } else {
            InitializeFirebase();
        }
void InitializeFirebase() {
        Debug.Log("Setting up Firebase Auth");
        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        auth.StateChanged += AuthStateChanged;
        FirebaseApp app = FirebaseApp.DefaultInstance;
        app.SetEditorDatabaseUrl("https://testapp-509c4.firebaseio.com/");
        mRef = FirebaseDatabase.DefaultInstance.RootReference;
    }
    // Track state changes of the auth object.
    void AuthStateChanged(object sender, System.EventArgs eventArgs) {
        if (auth.CurrentUser != user) {
            if (user == null && auth.CurrentUser != null) {
                Debug.Log("Signed in " + auth.CurrentUser.DisplayName);
            } else if (user != null && auth.CurrentUser == null) {
                Debug.Log("Signed out " + user.DisplayName);
            }
            user = auth.CurrentUser;
            //Debug.Log("Signed in " + auth.CurrentUser.DisplayName);

        }
    }

我不确定问题是什么。也许两个实例都尝试访问相同的权限或实例。每次我尝试auth.CreateUserWithEmailAndPasswordAsync(email, password)italicauth.SignInWithEmailAndPasswordAsync(username_text,password_text)等函数时,我都会在日志中收到此错误 在我尝试连接到firebase数据库之前,它工作得很好

每次我执行其中一项功能时,Android logcat都会显示此信息(即使它崩溃与否):

12-01 00:55:51.214 4615-4639/? I/Unity: NullReferenceException: Object reference not set to an instance of an object
    at Intro.Login () [0x00000] in <filename unknown>:0 
    at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in <filename unknown>:0 
    at UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
    at UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
    at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <filename unknown>:0 
    at UnityEngine.UI.Button.Press () [0x00000] in <filename unknown>:0 
    at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00000] in <filename unknown>:0 
    at UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <filename unknown>:0 
    at UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngi
12-01 00:55:51.224 21998-22429/? E/AsyncOperation: serviceID=16, operation=ValidateAuthServiceOperation
    java.lang.NullPointerException: onPostInitComplete can be called only once per call to getRemoteService
        at iri.a(:com.google.android.gms:74)
        at ioj.a(:com.google.android.gms:987)
        at ipf.a(:com.google.android.gms:66)
        at ixg.a(:com.google.android.gms:284)
        at eks.a(:com.google.android.gms:125)
        at eks.a(:com.google.android.gms:113)
        at ixn.run(:com.google.android.gms:113)
        at jaq.run(:com.google.android.gms:450)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at jew.run(:com.google.android.gms:17)
        at java.lang.Thread.run(Thread.java:841)

请帮忙,如果您需要我项目中的更多代码,请告诉我。 谢谢

3 个答案:

答案 0 :(得分:0)

我认为FirebaseApp.DefaultInstance.SetEditorDatabaseUrl注定要使用统一编辑器,所以你不应该包含Android。

尝试像这样包装:

#if UNITY_EDITOR
  FirebaseApp.DefaultInstance.SetEditorDatabaseUrl
#endif

答案 1 :(得分:0)

缺少Firebase统一编辑器参考。尝试添加:

using Firebase.Unity.Editor;

答案 2 :(得分:0)

Firebase Unity 最新版本已弃用 SetEditorDatabaseUrl

string  BaseURL = "https://yourapp-e756f.firebaseio.com/";

FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(BaseURL ); //Deprecated
   
//please comment the code.