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:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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.
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 asCreateFromImageAsyncfunction. So if we wantnew Texture2D()to be called on the main thread, we must callCreateFromImageAsyncon the main thread as well. I think that you try to callCreateFromImageAsyncon a thread other than the main thread.Therefore, something like this doesn’t work:
But, this works:
I tested the wrong way and reproduced the same error message.