【问题标题】:How to download image from Firebase storage? [closed]如何从 Firebase 存储下载图像? [关闭]
【发布时间】:2020-01-09 09:19:51
【问题描述】:


我正在尝试使用此示例从我的 Firebase 存储中下载和使用图像:

How to download image from firebase database, and load it into image in unity

Follow Debug 发现问题:

UnityEngine.UnityException: SupportsTextureFormatNative 只能从主线程调用。

你能提供一下,如何解决?或者,也许您可​​以提供不同的方式来下载和使用图像。
谢谢!

我的代码:

  var firebase = FirebaseStorage.DefaultInstance;
    var storageRef = firebase.GetReferenceFromUrl(_urlPics);

    storageRef.Child(_resourceName).GetBytesAsync(1024*1024).ContinueWith(task =>
    {
        if (task.IsCompleted)
        {
            var texture = new Texture2D(2, 2);
            byte[] fileContent = task.Result;

            texture.LoadImage(fileContent);
            var newRect = new Rect(0.0f, 0.0f, texture.width, texture.height);
            var sprite = Sprite.Create(texture, newRect, Vector2.zero);
            _image.sprite = sprite;
            Debug.Log("FINISH DOWNLOAD");
        }
        else
        {
            print("DOWNLOAD WRONG");
        }
    });

错误抛出:

var texture = new Texture2D(2, 2);

【问题讨论】:

  • 你能贴出产生这个错误的代码吗?
  • @LudovicFeltz 抱歉,只是发现这犯了一个大错误,因为该帖子显示了如何从数据库中获取链接。但我正在尝试找到如何从存储中获取图片的解决方案。但无论如何使用另一种方法都会遇到同样的问题。
  • 编辑了我在主帖中的错误。
  • @HassanVoyeau 程序员对您的链接非常好的解释谢谢,非常有趣:)

标签: c# image firebase unity3d firebase-storage


【解决方案1】:

我很快就得到了一个很好的解释(感谢 Pux0r3):

这是一个小更新,但将 ContinueWith 替换为 ContinueWithOnMainThread 会稍微改善这一点。

使用 .NET 4.x 运行时,延续可能经常在后台执行,而此扩展方法将它们推送到 主线程

.NET 3.x 运行时没有这个问题,因为 Parse 库中包含延续,它隐式地做到了这一点。

另请参阅 Firebase and Tasks, how to deal with asynchronous logic in Unity ,了解有关此方法和替代实施方式的更多信息。


所以虽然以前的ContinueWith“刚刚工作”。为了确保并且不必使用例如实现自定义工作者。 ConcurrentQueue<Action>(在 Unity 中将回调调度到主线程的一种常用方法)你应该只需将 ContinueWith 替换为 ContinueWithOnMainThread 中的 ContinueWithOnMainThread 就可以了。

【讨论】:

  • 非常感谢!我刚刚改变了,它的工作。
  • 你可以粘贴整个代码吗?我正在尝试这样做,但由于某种原因它不起作用
猜你喜欢
  • 2016-12-28
  • 2019-11-25
  • 1970-01-01
  • 2017-01-16
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多