0


I'm trying to download and use image from my Firebase storage using this example:

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

Follow Debug find a problem:

UnityEngine.UnityException: SupportsTextureFormatNative can only be called from the main thread.

Can you offer please, how to solve it? Or maybe you can offer different way to download and use image.
Thanks!

My code:

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

Error throw from:

var texture = new Texture2D(2, 2);
fidgetik
  • 7
  • 3

1 Answers1

1

I shortly got a nice explenation on this (thanks goes out to Pux0r3):

This is a minor update, but replacing ContinueWith with ContinueWithOnMainThread will improve this slightly.

With the .NET 4.x runtime, continuations may often execute in the background, whilst this extension method pushes them onto the main thread.

The .NET 3.x runtime didn't have this issue as continuations were contained within the Parse library, which did this implicitly.

Also see the Firebase and Tasks, how to deal with asynchronous logic in Unity for more information on this and alternative implementation ways.


So while former ContinueWith "just worked". In order to be sure and without having to implement a custom worker using e.g. a ConcurrentQueue<Action> (an often used approach for dispatching callbacks to the main thread in Unity) you should be fine by simply replacing ContinueWith with ContinueWithOnMainThread from the Firebase.Extensions.TaskExtension.

Community
  • 1
  • 1
derHugo
  • 83,094
  • 9
  • 75
  • 115