与 firebase 集成的 Unity 可在 Unity 编辑器上运行,但不适用于移动设备

2024-05-04

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

操作系统: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.


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

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 );

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

与 firebase 集成的 Unity 可在 Unity 编辑器上运行,但不适用于移动设备 的相关文章

随机推荐