首页 > 解决方案 > Google Firebase 和 Unity (C#):无法从存储桶下载 png

问题描述

眼镜

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

标签: c#firebaseunity3dfirebase-realtime-databasedownload

解决方案


为什么这对您不起作用可能有很多原因,包括:

  • 安全规则设置不正确
  • 文件路径不正确
  • 您在错误的平台上对其进行测试(Firebase 在编辑器中无法正常运行)
  • 您的设备阻止了连接
  • ETC...

为了获得错误消息,您需要记录它们:

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

请记住,在编辑器中测试它可能不起作用。


推荐阅读