【问题标题】:Unity integrated with firebase works on Unity Editor but not on mobile device与 firebase 集成的 Unity 可在 Unity 编辑器上运行,但不能在移动设备上运行
【发布时间】:2021-03-22 18:21:37
【问题描述】:

我在 Unity 2020.3.0.f1 上创建了一个简单的应用程序,并集成了 Firebase sdk 7.1.0。我基本上是在对 firebase 进行简单的读写操作。它在 Unity 编辑器中运行良好,但在移动版本中运行良好,尽管 apk 构建是 100% 成功的。

操作系统:Ubuntu 20.04(LTS)。

这是我的简单代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Firebase.Database;
using TMPro;

public class firebase_script : MonoBehaviour
{
    DatabaseReference reference;
    string text_place;
    public TextMeshProUGUI text;

    // Start is called before the first frame update
    void Start()
    {
        reference= FirebaseDatabase.DefaultInstance.RootReference;
    }
    public void get_updates(){
        reference.Child("plant").GetValueAsync().ContinueWith(task => {
            if(task.IsFaulted){
                Debug.Log("Failed to fetch Value");
            }
            else if(task.IsCompleted){
                DataSnapshot snapshot =task.Result;
                text_place=snapshot.Child("moisture").Value.ToString();
                
            }
        });
    }

    // Update is called once per frame
    void Update()
    {
        text.text=text_place;   
    }
}

编辑:当我看到 logcat 时,我发现 Firebase 应用程序初始化失败,但在 Unity 编辑器中工作时看不到此类错误,并且该应用程序在 Unity 编辑器中运行良好,但在 Android 设备中却没有

03-23 00:45:45.253  9618  9707 I Unity   : Company Name: <Striped>
03-23 00:45:45.253  9618  9707 I Unity   : Product Name: <Striped>
03-23 00:45:48.719  9618  9707 E Unity   : InitializationException:  Firebase app creation failed.
03-23 00:45:48.719  9618  9707 E Unity   :   at Firebase.FirebaseApp.CreateAndTrack (Firebase.FirebaseApp+CreateDelegate createDelegate, Firebase.FirebaseApp existingProxy) [0x000e3] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.719  9618  9707 E Unity   :   at Firebase.FirebaseApp.Create () [0x00027] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.719  9618  9707 E Unity   :   at Firebase.FirebaseApp.get_DefaultInstance () [0x00017] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.719  9618  9707 E Unity   :   at Firebase.Database.FirebaseDatabase.get_DefaultInstance () [0x00000] in <265179cd3d324ba1be0c68f88dea310a>:0 
03-23 00:45:48.719  9618  9707 E Unity   :   at firebase_script.Start () [0x00000] in <a8ca21255e29484580e851c97ded26fa>:0 
03-23 00:45:48.719  9618  9707 E Unity   : 
03-23 00:45:48.739  9618  9707 E Unity   : InitializationException:  Firebase app creation failed.
03-23 00:45:48.739  9618  9707 E Unity   :   at Firebase.FirebaseApp.CreateAndTrack (Firebase.FirebaseApp+CreateDelegate createDelegate, Firebase.FirebaseApp existingProxy) [0x000e3] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.739  9618  9707 E Unity   :   at Firebase.FirebaseApp.Create () [0x00027] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.739  9618  9707 E Unity   :   at Firebase.FirebaseApp.get_DefaultInstance () [0x00017] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.739  9618  9707 E Unity   :   at Firebase.Database.FirebaseDatabase.get_DefaultInstance () [0x00000] in <265179cd3d324ba1be0c68f88dea310a>:0 
03-23 00:45:48.739  9618  9707 E Unity   :   at button_script.Start () [0x00000] in <a8ca21255e29484580e851c97ded26fa>:0 
03-23 00:45:48.739  9618  9707 E Unity   : 
03-23 00:45:48.759  9618  9707 E Unity   : InitializationException:  Firebase app creation failed.
03-23 00:45:48.759  9618  9707 E Unity   :   at Firebase.FirebaseApp.CreateAndTrack (Firebase.FirebaseApp+CreateDelegate createDelegate, Firebase.FirebaseApp existingProxy) [0x000e3] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.759  9618  9707 E Unity   :   at Firebase.FirebaseApp.Create () [0x00027] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.759  9618  9707 E Unity   :   at Firebase.FirebaseApp.get_DefaultInstance () [0x00017] in <efce830506c14731bd3b7a14d631487d>:0 
03-23 00:45:48.759  9618  9707 E Unity   :   at Firebase.Database.FirebaseDatabase.get_DefaultInstance () [0x00000] in <265179cd3d324ba1be0c68f88dea310a>:0 
03-23 00:45:48.759  9618  9707 E Unity   :   at button_script.Start () [0x00000] in <a8ca21255e29484580e851c97ded26fa>:0 
03-23 00:45:48.759  9618  9707 E Unity   : 
03-23 00:45:48.830  9618  9707 E Unity   : Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly.
03-23 00:45:48.830  9618  9707 E Unity   : 
03-23 00:45:48.830  9618  9707 E Unity   : Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly.

【问题讨论】:

  • 什么不工作,有什么错误吗?
  • 这就是问题所在。在 Unity 编辑器中单击按钮,它会执行 get_updates() 并将值保存在 text_place 中,该值稍后会在 text.text 中获取更新。但是当我在移动设备上构建相同的版本时,它构建得非常好,但现在它没有从实时数据库中获取数据

标签: c# android firebase unity3d firebase-realtime-database


【解决方案1】:

我遇到了与未加载 google-services.json 相同的问题(Unity 2021.1.4f),我花了很长时间寻找答案,但没有找到可行的建议解决方案。最后,我尝试使用从 google-services.json 复制的值手动创建的 AppOptions 手动调用 Firebase.Create()。

Firebase.AppOptions options = new Firebase.AppOptions();
options.ApiKey = "XXXXXXXXXXX";
options.AppId = "XXXXXXXXXXXX";
options.MessageSenderId = "XXXXXXXXXXXXX";
options.ProjectId = "my-app";
options.StorageBucket = "my-app.appspot.com";
    
var app = Firebase.FirebaseApp.Create( options );

不是一个理想的解决方案,但在修复此错误之前一直有效。

【讨论】:

    【解决方案2】:

    根据您的日志,您缺少将游戏连接到 Firebase 后端所需的 google-services.json 文件。这可能有多种原因和多种解决方法。

    最简单的解决方案:

    确保在您的Assets/ 目录中的某处有您的google-services.json 文件。如果您忘记下载,可以从 Firebase 信息中心下载。

    来自this page

    为您的 Android 应用获取配置文件

    在 Firebase 控制台中转到您的 Project settings

    在您的应用卡中,选择您需要配置文件的应用的包名称。

    点击 google-services.json。

    针对 Unity 进行了修改: 将配置文件移动到游戏的 Assets/ 目录中。

    确保您的应用中只有这个最近下载的配置文件。

    google-services.json 存在,我仍然收到错误

    确保您已安装 python,并且您的路径中有一个可执行文件实际上名为 python 而不是 python3

    一般Android开发者会使用play services gradle插件将google-services.json转成xml(过程详见here)。 Firebase Unity 插件在 2017 年仍然支持 Unity,这早于对游戏中 gradle 的稳定支持(它曾经支持回 5 分支,但它已经足够老了,很难找到仍然运行编辑器的测试机器)。因此,Firebase Unity 插件改为运行名为generate_xml_from_google_services_json.py(在Windows 上编译为generate_xml_from_google_services_json.exe)的python 脚本,该脚本生成文件Assets/Plugins/Android/FirebaseApp.androidlib/res/values/google-services.xml。这通常是当您在 Unity 上下文中看到错误提示您缺少 google-services.json 文件时缺少的内容。

    此脚本与 Python 3 和 2 交叉兼容(2 因为它仍然在 MacOS 中提供),但该工具会尝试在您的路径中使用 python 来执行它。由于您使用的是 Linux,因此您可能想尝试戳一下这个错误:https://github.com/firebase/quickstart-unity/issues/1005(如果您认为应该打开它并且没有人响应,请在 cmets 中提及 @patm1987——提醒我这个答案)。

    这些都不起作用

    我建议打开一个错误 here,但对于所有与 google-service.json 相关的问题都有解决方法。

    请注意,我建议不要将此作为第一步,因为这种配置不是预期的,并且可能会让人们陷入循环试图在未来帮助您。由于 Unity 和 Firebase 的交互方式,它也有点脆弱。

    当您在 Firebase 类上调用 FirebaseApp.DefaultInstance 或任何其他 .DefaultInstance 时,如果没有具有默认应用名称的 FirebaseApp,则隐式延迟调用 Firebase.Create()。 @ 987654327@,一旦创建默认应用程序(我的意思是这个过程很脆弱),您就无法覆盖默认应用程序,但是如果您在创建默认应用程序之前调用.Create(AppOptions)(在任何线程中调用.DefaultInstance之前) ),您可以提供自己的应用选项。

    然后,您可以将 google-services.json 推入 StreamingAssets/ 目录并直接加载它,或者通过复制/粘贴 google-services.json 中的字段来加载 build an AppOptions

    再次,我建议这是你最后的手段,而不是你的第一个。如果你必须这样做,我会问你file a bug

    【讨论】:

      猜你喜欢
      • 2019-01-28
      • 2017-11-22
      • 2020-08-01
      • 2018-03-04
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多