使用Firebase 4.2的Unity 2017。 App在Unity Editor中运行,但在Android上运行应用程序时失败。 Firebase连接错误

时间:2017-12-11 23:18:22

标签: firebase unity3d

我正在使用Unity 2017.2.0f3和Firebase SDK 4.2.1,当我在Unity编辑环境中在PC上运行它时,当我将它构建到apk并在Android设备上运行时,Firebase会给我一个错误。

我的代码:

    FirebaseApp.DefaultInstance.SetEditorP12FileName("xxxxxxxxxxxxxxx-admin-sdk.p12"); 
 FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("xxxxxxxxxxxxxxx.iam.gserviceaccount.com"); 
    FirebaseApp.DefaultInstance.SetEditorP12Password("notasecret");
    FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
    dependencyStatus = task.Result;
    if (dependencyStatus == DependencyStatus.Available) {
    InitializeFirebase();
    } else {
    Debug.LogError(
    "Could not resolve all Firebase dependencies: " + dependencyStatus);
    }
    });

我正在使用服务帐户进行连接,当我运行应用程序时,它在Unity Editor上运行良好。但是当我生成APK并将其安装在Android设备(Samsung S7,Android 7.0)上时,当我运行应用程序时,LogCat会给我这个错误:

11-22 21:23:58.819: E/Unity(5835): Firebase.Database.Internal.Core.ValueEventRegistration:FireCancelEvent(DatabaseError)
11-22 21:23:58.819: E/Unity(5835): Firebase.Database.Internal.Core.View.CancelEvent:Fire()
11-22 21:23:58.819: E/Unity(5835): Firebase.Database.Internal.Core.View.Runnable30:Run()

此链接也会报告错误:https://github.com/firebase/quickstart-unity/issues/120

欢迎任何帮助......

1 个答案:

答案 0 :(得分:1)

嗯......我犯了几个错误,最后我修好了。由于某种原因,在您的项目上设置服务帐户以从Firebase实时数据库读取/写入数据是不够的。它在您运行游戏时适用于Unity Editor。但是,如果您不是移动设备上的APK(Android),App(游戏)必须验证有效的Firebase用户

我在Firebase控制台上创建了一个用户签名方法EMAIL / PASSWORD。然后我使用Firabase Authetication Unity Package对我的应用程序进行了身份验证,而Voila ......它的工作原理。 当用户在Firebase上进行身份验证时,Firebase系统会自动将实时数据库规则更改为TRUE

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
} 

https://firebase.google.com/docs/database/unity/start

我的工作代码,即使在Unity Editor和移动设备上也是如此:

private Firebase.Auth.FirebaseAuth auth;
private Firebase.Auth.FirebaseUser newUser;

void Awake() {
    //Using Firebase SDK
    //This is needed only for the unity editor
    FirebaseApp.DefaultInstance.SetEditorP12FileName("xxxxxxxxxxxxxxxxxxx-admin-sdk.p12");
    FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("xxxxxxxxxxxxxxxx@yyyyyyyyyyyyyyyyy.gserviceaccount.com");
    FirebaseApp.DefaultInstance.SetEditorP12Password("notasecret");
    //--------------------------------------

    FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
        dependencyStatus = task.Result;
        if (dependencyStatus == DependencyStatus.Available) {
            InitializeFirebase();
        } else {
            Debug.LogError(
              "Could not resolve all Firebase dependencies: " + dependencyStatus);
        }
    });
}

// Initialize the Firebase database:
protected virtual void InitializeFirebase() {
    FirebaseApp app = FirebaseApp.DefaultInstance;
    // NOTE: You'll need to replace this url with your Firebase App's database
    // path in order for the database connection to work correctly in editor

    //This is needed only for the unity editor
    app.SetEditorDatabaseUrl("https://xxxx.yyyyyyyyy.com/");
    if (app.Options.DatabaseUrl != null) app.SetEditorDatabaseUrl(app.Options.DatabaseUrl);
    //--------------------------------------
    Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    auth.SignInWithEmailAndPasswordAsync("mail@mail.com", "strongpassword").ContinueWith(task => {
        if (task.IsCanceled) {
            Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
            return;
        }
        if (task.IsFaulted) {
            Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
            return;
        }
        Firebase.Auth.FirebaseUser newUser = task.Result;
        Debug.LogFormat("User signed in successfully: {0} ({1})",
            newUser.DisplayName, newUser.UserId);
        //Getting client id for FB using device id
        FirebaseDatabase.DefaultInstance.GetReference("node1").Child(node2).Child("node3")
        .ValueChanged += HandleChanged;
    });
}

注意:密码应该很强。 不要忘记在destroy方法上注销,否则下次尝试连接时会出现错误。

auth.SignOut();