question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CreateFromImageAsync Texture2D does not work async / SupportsTextureFormatNative error

See original GitHub issue

@Looooong I get the following error when calling CreateFromImageAsync from a thread:

UnityException: SupportsTextureFormatNative can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene.

Unity 2020.3.1. AFAIK it’s impossible to create a new Texture2D on anything but the main thread? Though that seems to render this whole repo useless, which I hope isn’t the case cuz it looks GREAT otherwise!

Here’s the full stack trace:

UnityException: SupportsTextureFormatNative can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.EditorSystemInfo.SupportsTextureFormat (UnityEngine.TextureFormat format) (at <10564ed154d647e194bef4aef8878649>:0)
UnityEngine.SystemInfoShimBase.SupportsTextureFormat (UnityEngine.TextureFormat format) (at <10564ed154d647e194bef4aef8878649>:0)
UnityEngine.SystemInfo.SupportsTextureFormat (UnityEngine.TextureFormat format) (at <10564ed154d647e194bef4aef8878649>:0)
UnityEngine.Texture.ValidateFormat (UnityEngine.TextureFormat format) (at <10564ed154d647e194bef4aef8878649>:0)
UnityEngine.Texture2D..ctor (System.Int32 width, System.Int32 height, UnityEngine.TextureFormat textureFormat, System.Int32 mipCount, System.Boolean linear, System.IntPtr nativeTex) (at <10564ed154d647e194bef4aef8878649>:0)
UnityEngine.Texture2D..ctor (System.Int32 width, System.Int32 height, UnityEngine.TextureFormat textureFormat, System.Int32 mipCount, System.Boolean linear) (at <10564ed154d647e194bef4aef8878649>:0)
AsyncImageLoader+ImageImporter+<CreateNewTextureAsync>d__17.MoveNext () (at Assets/UnityAsyncImageLoader/Runtime/ImageImporter.cs:100)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () (at <eae584ce26bc40229c1b1aa476bfa589>:0)
AsyncImageLoader+<CreateFromImageAsync>d__9.MoveNext () (at Assets/UnityAsyncImageLoader/Runtime/AsyncImageLoader.cs:105)
UnityEngine.Debug:LogException(Exception)
<CreateFromImageAsync>d__9:MoveNext() (at Assets/UnityAsyncImageLoader/Runtime/AsyncImageLoader.cs:108)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()

and the method I’m calling it from:

private static async Task<Texture2D> ByteArrayToTexture2D(byte[] bytes)
{
    return await AsyncImageLoader.CreateFromImageAsync(bytes);
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mdsittoncommented, Apr 13, 2022

Yeah i tried a few other things with the same result. In my game we have a similar library that we put together and we just create the texture2d on the main thread and get GetRawTextureData and give that to a Task to load into without stalling the main thread. The actual calling code isnt async just the internals.

0reactions
Looooongcommented, Dec 15, 2022

Now that I take a look at the code again, I find it weird, because new Texture2D() constructor is supposed to be called on the same thread as CreateFromImageAsync function. So if we want new Texture2D() to be called on the main thread, we must call CreateFromImageAsync on the main thread as well. I think that you try to call CreateFromImageAsync on a thread other than the main thread.

Therefore, something like this doesn’t work:

  void Start() {
    Task.Run(() => CreateFromImageAsync(data));
  }

But, this works:

  async void Start() {
    await CreateFromImageAsync(data);
  }

I tested the wrong way and reproduced the same error message.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · Looooong/UnityAsyncImageLoader
Asynchronous Image Loader for Unity. ... CreateFromImageAsync Texture2D does not work async / SupportsTextureFormatNative error good first issue Good for ...
Read more >
Resolved - Async version of Texture2D.GetPixel()
The only problem is that i like my terrain generator to compute asynchronously to the main thread. Unfortunately the Texture2D.
Read more >
c# - Texture2D.LoadImage async
Textures created by C# must be created in the main thread. You could try setting Texture.allowThreadedTextureCreation = true but that won't work ......
Read more >
Async operation handling | Addressables | 1.14.3
When you no longer need the asset provided by a returned AsyncOperationHandle struct, you should release it using the Addressables.
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
The async function declaration creates a binding of a new async function to a given name. The await keyword is permitted within the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found