在Unity上的场景中使用Firebase云存储中存储的音频/图像

问题描述

我目前正在开发AR应用程序,在某些地方我想通过显示位置的图像和描述来显示有关特定地理位置的信息。我想从Firebase检索图像和描述,但是我不确定该怎么做。还有另一个场景,我想从Firebase云存储中检索音频文件以在场景中播放。非常感谢您的帮助!

谢谢:)

解决方法

有多种方法可以从Cloud Storage下载文件。我的建议是,如果您刚刚开始,请观看this video概述与Unity的集成。

您基本上将有三种方法:

  1. 使用Unity SDK下载byte arraystream。如果您在内存中有空间来容纳它,则可能是首选。
// Download in memory,make the max size reasonable for your game
reference.GetBytesAsync(long.MaxValue).ContinueWithOnMainThread((Task<byte[]> task) => {
  if (task.IsFaulted || task.IsCanceled) {
    Debug.Log(task.Exception.ToString());
    // Uh-oh,an error occurred!
  } else {
    byte[] fileContents = task.Result;

    // this is needed for Unity
    Texture2D tex = new Texture2D(2,2);
    tex.LoadImage(fileContents);
  }
});
  1. Download to a file,稍后加载
// Create local filesystem URL
string local_url = $"file://{Application.persistentDataPath}/imageName.jpg";

// Download to the local filesystem
reference.GetFileAsync(local_url).ContinueWithOnMainThread(task => {
    if (!task.IsFaulted && !task.IsCanceled) {
        Debug.Log("File downloaded.");
        // load from disk later
    }
});
  1. Download from a URL,通常不建议这样做,但是可以将一些工作转移到Unity。
reference.GetDownloadUrlAsync().ContinueWithOnMainThread((Task<Uri> task) => {
  if (!task.IsFaulted && !task.IsCanceled) {
    Debug.Log("Download URL: " + task.Result());
    // ... now download the file via WWW or UnityWebRequest.
  }
});
  1. 完全忽略Cloud Storage SDK,只需从控制台获取“公共URL”即可。使用Unity的UnityWebRequest将其下拉。