简体   繁体   English

在一个项目中使用数据库和身份验证包时出现 Unity Firebase [to Android] 错误

[英]Unity Firebase [to Android] error when using database and auth package in one project

I'm developing an app for Android in Unity3d that uses Firebase Authentication, and it worked just fine until I tried to use the Firebase database package.我正在 Unity3d 中开发一个使用 Firebase 身份验证的 Android 应用程序,它运行良好,直到我尝试使用 Firebase 数据库包。 It imported just fine.它进口得很好。 But when I typed code to set my database in the Start() function for my app such as FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-Firebase-APP.Firebaseio.com/");但是当我在 Start() 函数中为我的应用程序输入代码来设置我的数据库时,例如FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-Firebase-APP.Firebaseio.com/"); (wich is in the Firebase unity guide https://Firebase.google.com/docs/database/unity/start ). (在 Firebase 统一指南https://Firebase.google.com/docs/database/unity/start 中)。 it crashes almost everytime I use authentication or database functions, or a few seconds after.几乎每次我使用身份验证或数据库功能或几秒钟后它都会崩溃。

This is my start() function before I added the database package:这是我添加数据库包之前的 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);

        }
    }

And this is my code after I applied the database connection (I just added a variable at the beggining and few lines in the InitializeFirebase() Function):这是我应用数据库连接后的代码(我刚刚在 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);

        }
    }

I'm not sure what the problem is.我不确定问题是什么。 Maybe both instances try to access the same permission or instance.也许两个实例都尝试访问相同的权限或实例。 I get this error on the log everytime I try functions like auth.CreateUserWithEmailAndPasswordAsync(email, password) OR italicauth.SignInWithEmailAndPasswordAsync(username_text,password_text) wich worked fine before I try to connect to firebase database每次尝试auth.CreateUserWithEmailAndPasswordAsync(email, password) OR italicauth.SignInWithEmailAndPasswordAsync(username_text,password_text)类的功能时,我都会在日志中收到此错误,然后我尝试连接到 firebase 数据库

The Android logcat shows this everytime I do one of those functions (even if it crashes or not):每次我执行其中一项功能时,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)

Please help, if you need more code from my project just let me know.请帮助,如果您需要我的项目中的更多代码,请告诉我。 Thank You谢谢你

I think FirebaseApp.DefaultInstance.SetEditorDatabaseUrl is destined for the unity editor so Android you should not have that included.我认为FirebaseApp.DefaultInstance.SetEditorDatabaseUrl是用于统一编辑器的,因此您不应该将其包含在 Android 中。

Try wrapping it like this:尝试像这样包装它:

#if UNITY_EDITOR
  FirebaseApp.DefaultInstance.SetEditorDatabaseUrl
#endif

It's missing the firebase unity editor reference.它缺少 firebase 统一编辑器参考。 Try adding:尝试添加:

using Firebase.Unity.Editor;

Firebase Unity Latest Version Deprecated SetEditorDatabaseUrl Firebase Unity 最新版本已弃用 SetEditorDatabaseUrl

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 firebase unity sdk firestore package 时 android 设备出错 - Error on android device when using firebase unity sdk firestore package 使用Firebase Auth在Android上执行Flutter项目时出错 - Error to execute Flutter project on Android using Firebase Auth Firebase的Unity Auth错误 - Unity Auth Error With Firebase 在使用 Firebase Auth 的 Unity 项目中使用 Google Drive API - Using the Google Drive API in a Unity project that uses Firebase Auth Android studio firebase auth uid 在数据库中使用 - Android studio firebase auth uid using in database 错误:无法解决:同步Android项目时,firebase-auth-license - Error:Failed to resolve: firebase-auth-license when synchronizing Android project 除Android应用程序添加到的项目以外的其他项目上的Firebase Auth - Firebase Auth on a project other than the one the Android application is added to Firebase Android 身份验证错误 - Firebase Android Auth Error 在Android Studio中构建统一项目(具有Firebase)时出错 - Error while building unity project (having firebase) in Android studio 在Android Studio中构建统一项目(具有Firebase)时出错 - Error while building unity project(having firebase) in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM