- Home /
How to set size to a Texture2D after an await?
I am using Unity in Hololens with UWP, so I have a few async/await calls. However, when I try to do;
var height = await ...;
var width = await ...;
var foo = new Texture2D(width, height);
it throws an exception
Internal_Create 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.
How can I still construct a Texture2D after an await call when the necessary information is available?
Answer by FlaSh-G · Jun 19, 2017 at 03:27 PM
You can't change your Unity world in another thread than the main thread. You will have to write your results into some variable and wait in Update or a coroutine to use them. Feel free to use this class like this:
void Start()
{
StartCoroutine(DoStuff());
}
private IEnumerator DoStuff()
{
var height = 0;
var width = 0;
yield return new CoroutineThread(() =>
{
// Do Thread stuff here
});
ResultStuff(width, height);
}
Your answer
Follow this Question
Related Questions
Using UWP Universal Windows Platform API in Unity 2 Answers
IAP Windows Universal 0 Answers
[macOS] cant build windows version,[macOS] Cant build windows version 0 Answers
How to use 2 different Unity software in Windows? 3 Answers
Is Unity working on Windows 10? 11 Answers