【发布时间】: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