与 firebase 集成的 Unity 适用于 Unity Editor,但不适用于移动设备 [英] Unity integrated with firebase works on Unity Editor but not on mobile device
问题描述
我在 Unity 2020.3.0.f1 上创建了一个简单的应用程序,并集成了 Firebase sdk 7.1.0.我基本上是对firebase进行简单的读写.尽管 apk 构建 100% 成功,但它在 Unity 编辑器中运行良好,但不适用于移动构建.
I have created a simple app on Unity 2020.3.0.f1 with integration of Firebase sdk 7.1.0. I am basically doing a simple read and write to firebase. It's working well in Unity editor but not in a mobile build although the apk build is 100% successful.
操作系统:Ubuntu 20.04(LTS).
Operating System: 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 编辑器中运行良好,但在 android 设备中却没有
When I see the logcat I find that Firebase app initialisation fails but no such error can be seen while working in unity editor and the app worked fine in Unity editor but not in android device
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) 相同的问题,我花了很长时间寻找答案,但没有建议的解决方案发现工作.最后,我尝试使用从 google-services.json 复制的值手动创建的 AppOptions 手动调用 Firebase.Create().
I had the same issue with google-services.json not being loaded (Unity 2021.1.4f) and spent a looong time looking for answers and no suggested solutions I found worked. Finaly I tried calling Firebase.Create() manualy with manualy created AppOptions with values copied from 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 );
不是一个理想的解决方案,但在此错误得到修复之前一直有效.
Not an ideal solution but works till this bug gets fixed.
这篇关于与 firebase 集成的 Unity 适用于 Unity Editor,但不适用于移动设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!