在一个项目中使用数据库和auth软件包时,Unity Firebase [to Android]错误



我正在为Unity3D中的Android开发一个使用Firebase Authentication的应用程序,并且它的工作正常,直到我尝试使用Firebase数据库包。进口很好。但是,当我键入代码将数据库设置在start()函数中的数据库时(例如FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-Firebase-APP.Firebaseio.com/");)(WICH位于firebase Unity指南中https://firebase.google.com/docs/database/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)

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

我认为 FirebaseApp.DefaultInstance.SetEditorDatabaseUrl注定是为unity编辑器,所以您不应该包含其中的android。

尝试像这样包装:

#if UNITY_EDITOR
  FirebaseApp.DefaultInstance.SetEditorDatabaseUrl
#endif

它缺少Firebase Unity编辑器参考。尝试添加:

using Firebase.Unity.Editor;

firebase Unity最新版本不弃用设置ditodordatabaseurl

string  BaseURL = "https://yourapp-e756f.firebaseio.com/";
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(BaseURL ); //Deprecated
   
//please comment the code.

最新更新