我正在 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)
请帮忙,如果您需要我的项目中的更多代码,请告诉我。 谢谢你
我认为
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl
是为 Unity 编辑器设计的,所以 Android 上你不应该包含它。
尝试像这样包裹它:
#if UNITY_EDITOR
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl
#endif
缺少 firebase unity 编辑器参考。尝试添加:
using Firebase.Unity.Editor;
Firebase Unity 最新版本已弃用 SetEditorDatabaseUrl
string BaseURL = "https://yourapp-e756f.firebaseio.com/";
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(BaseURL ); //Deprecated
//please comment the code.