【问题标题】:Using audio/images stored on Firebase cloud storage in my scene on Unity在我的 Unity 场景中使用存储在 Firebase 云存储中的音频/图像
【发布时间】:2020-12-07 03:42:35
【问题描述】:

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

谢谢:)

【问题讨论】:

    标签: firebase unity3d firebase-realtime-database firebase-storage


    【解决方案1】:

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

    不过,您基本上可以采用三种方法:

    1. 使用 Unity SDK 下载 byte array 或 stream。如果您有内存空间来保存它,这可能是首选。
    // 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 将其拉下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2021-12-20
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2020-12-04
      相关资源
      最近更新 更多