规范
Unity editor version: 2018.2.8f1
Firebase Unity SDK version: 5.5.0
Additional SDKs: SimpleFirebaseUnity
Developing on: Mac
Export Platform: Android
问题
我在设置系统以从存储中下载图片时遇到问题。我不是数据库专家,但我想尝试一下,只是为了了解它是如何完成的。 我发现 Firebase 对于在实时数据库上存储元数据非常有用,而且即使对于像我这样的入门级程序员来说也很容易上手。
问题是我正在尝试从存储中的文件夹下载 .png
文件,但我无法确定该文件是否已实际下载,或者它是否只是丢失了过程。我在控制台中没有收到任何错误,但是当我打开文件所在的文件夹时,它是空的。
代码
private SimpleFirebaseUnity.Firebase firebaseDatabase;
private FirebaseQueue firebaseQueue;
private FirebaseStorage firebaseStorage;
private StorageReference m_storage_ref;
// Setup refernece to database and storage
void SetupReferences()
{
// Get a reference to the database service, using SimpleFirebase plugin
firebaseDatabase = SimpleFirebaseUnity.Firebase.CreateNew(FIREBASE_LINK, FIREBASE_SECRET);
// Get a reference to the storage service, using the default Firebase App
firebaseStorage = FirebaseStorage.DefaultInstance;
// Create a storage reference from our storage service
m_storage_ref = firebaseStorage.GetReferenceFromUrl(STORAGE_LINK);
// Create a queue, using SimpleFirebase
firebaseQueue = new FirebaseQueue(true, 3, 1f);
}
// ...
IEnumerator DownloadImage(string address, string fileName)
{
var local_path = Application.persistentDataPath + THUMBNAILS_PATH;
var content_ref = m_storage_ref.Child(THUMBNAILS_PATH + fileName + ".png");
content_ref.GetFileAsync(local_path).ContinueWith(task => {
if (!task.IsFaulted && !task.IsCanceled)
{
Debug.Log("File downloaded.");
}
});
yield return null;
}
最佳答案
这对您不起作用的原因可能有很多,包括:
- 安全规则设置不当
- 文件路径不正确
- 您在错误的平台上进行测试(Firebase 在编辑器中运行不佳)
- 您的设备正在阻止连接
- 等...
为了获得错误消息,您需要记录它们:
IEnumerator DownloadImage(string address, string fileName)
{
var local_path = Application.persistentDataPath + THUMBNAILS_PATH;
var content_ref = m_storage_ref.Child(THUMBNAILS_PATH + fileName + ".png");
content_ref.GetFileAsync(local_path).ContinueWith(task => {
if (!task.IsFaulted && !task.IsCanceled)
{
Debug.Log("File downloaded.");
}
else
{
Debug.Log(task.Exception.ToString());
}
});
yield return null;
}
请记住,在编辑器中对其进行测试可能无法正常工作。
关于c# - 谷歌 Firebase 和 Unity (C#) : Unable to download png from bucket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55283247/