- Home /
yield www loading local file blocks UI
I'm having to do a lot of texture loading from file at the start of each scene (these are files downloaded at run time at an earlier point, so can't be in resources folder)
I'm using this method to do it:
var www = new WWW("file://"+path);
yield return www;//wait until the file is loaded
Texture2D texture = new Texture2D(4,4,TextureFormat.BGRA32,true);
www.LoadImageIntoTexture(texture);
Which all works, but the problem is that it seems to block the UI, so that my loading spinner stops spinning while it's happening. I thought the yield would prevent this (this method seems to work fine when downloading a file from web, it's only when loading a local file that the UI blocking happens).
I checked in the profiler, and when the texture loading happens, there is a huge spike, and almost all of the CPU is being used in: MyTextureLoadingCoroutine() [Coroutine: MoveNext] which suggests that the thing blocking the UI is actually the yield?
Unless i'm doing something wrong this basically makes www for local files practically useless... am i going to have to write my own image file loading from scratch, reading pixel data into the texture? This way i can just run it all on a background thread. The www stuff can't go on a background thread, because wwwInit can only be called from the main thread.
Why dont you use simple File/StreamReader to read those file if they are stored localy? If they are stored localy this should be a lot faster and easier to perform.
I suppose it's just a lot more coding? would i have to manually load the pixel data into a low level c# image object of some kind, and then somehow create a Texture2D out of that?
ok i changed it to load the bytes with FileStream, and then LoadImage on the Texture2D. But it turns out no Texture2D stuff can be called from a background thread! So it locks up the GUI just as much and i'm completely stuck again.
$$anonymous$$aybe a stupid question, but in your example are you calling that code via a coroutine? I'm very surprised that a coroutine would block UI code elsewhere.
Depending on how many assets you have, it could be the other functions, like turning the downloaded info into textures, that's blocking your app.
Try a slimmed down version of the app that just downloads the files in a coroutine without doing anything to them and see if that still blocks.
@VesuvianPrime : He mentions using a coroutine in his description. Coroutines can still block since they're run in the same thread, they're just functions that are run at the end of the frame (usually) and that can be paused for later. If they take too long in between yields they can hang a program like anything else that hogs the computer's time.
Your answer
Follow this Question
Related Questions
Another yield and www question 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
yield return www - What is www referring to? 2 Answers
yield return request never returns 2 Answers