无法使用 Firebase 配置保存和加载



我遵循了所有步骤,例如将数据库SDK添加到Unity等,但是当我尝试将Unity连接到Firebase时,我遇到了错误。

资产\脚本\实时加载.cs(14,21):错误 CS0029:无法将类型"Firebase.Database.DatabaseReference"隐式转换为"数据库引用">

我遵循了很多教程,他们从来没有收到错误,只有我。这是我的代码

using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RealTimeLoading : MonoBehaviour
{
DatabaseReference reference;
// Start is called before the first frame update
void Start()
{
reference = FirebaseDatabase.DefaultInstance.RootReference;



}
// Update is called once per frame
void Update()
{

}
}

错误位于此行:

FirebaseDatabase.DefaultInstance.RootReference;

我做错了什么?

在简要浏览了您的using陈述之后,我认为除了Firebase.Database中的陈述之外,不应该有其他DatabaseReference。这让我认为您在项目的根命名空间中还有另一个同名class

首先,我建议删除Firebase.Unity.Editor,如果仍然有一些文档推荐它,请务必告诉我。

然后你应该能够简单地写:

using DatabaseReference = Firebase.Database.DatabaseReference;

与您的其他using指令。因此,文件的顶部可能如下所示:

using Firebase;
using Firebase.Database;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DatabaseReference = Firebase.Database.DatabaseReference;

请注意这一点,因为如果代码库中有另一个名为DatabaseReference的类,则可能会更频繁地遇到此命名冲突。如果可能,将其移动到自己的命名空间或重命名它可能是有益的。但这应该会让你立即摆脱困境。

或者:您可以完全限定类中的名称,而不是添加using别名。例如:

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

public class RealTimeLoading : MonoBehaviour
{
Firebase.Database.DatabaseReference reference;
// Start is called before the first frame update
void Start()
{
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
}

我建议不要在一个文件中同时执行这两个操作,但是您可能必须在不同情况下使用它们。

最新更新