- Home /
Does UnityEngine.CustomeYieldInstruction works in a seperate thread?
As we know coroutines works in the main thread and i read somewhere that by implementing customyieldinstruction you can wait for something to complete in a separate thread. Is this true? Will it not freeze or slows down my app??
i have never done that, but feels a bit weird, how are you suppost to start the coroutine from a separated thread? is there any need to use the coroutine in a separated thread for something?
i have to load almost 100 images on my home screen but yield return freezes the app. Do you have any solution in $$anonymous$$d ?
what do you mean by load? download from somewhere? can you share your code so we can check where you could optimize it?
Please do not post an answer when you don't answer your own question. If you want to add more details / information to your question, just edit the question.
I'm with xxmariofer. Without seeing your code we can't tell much. Generally it's possible to wait for a "signal" using a CustomYieldInstruction. However this requires a boolean value that the yield instruction can check and that is set by the thread once finished.
$$anonymous$$eep in $$anonymous$$d that you can not use the Unity API from a different thread than the main thread. So the final call to LoadImage must be done on the main thread. The LoadImage might be the actual bottleneck of your issue
Answer by anilhdas · Apr 06, 2019 at 09:44 AM
Does CustomYieldInstruction
work in a seperate thread?
No. It doesn't
We know that any Unity API cannot be accessed from a seperate thread.
And CustomYieldInstruction
is just another Unity API except the fact that you're now yielding a custom event (loading an image) instead of an Unity event (an update cycle or fixed time).
As per the documentation, keepWaiting
property (defined in your CustomYieldInstruction class) is queried each frame after Update and before LateUpdate call.
Although as @Bunny83 has pointed out, you can possibly do custom logic such as downloading the image, using your own thread, loading it into memory (through Resources.Load()
for ex) can only be done using Unity's main thread.
Same behaviour can also be achieved using WaitUntil
or WaitWhile
instructions. However if logic to yield is complex it's good to use CustomYieldInstruction to make the code easy to read and maintain.
Answer by zulqarnain26 · Apr 07, 2019 at 05:16 PM
I just want to download high res images but don't want the unity to freeze? Is there any solution? Will async/awaiy work?
I have asked you multiple times what was your code, since downloadinh the image maybe is not causing the freeze, a lot of times creating all 100 sprites at runtime is more expensive than just downloading the image and the issur might be as easy as using rawimage, also most likely yoy dont need the 100 imahes all at the same exact tiem and you can go downloading 10 by 10 for example
I don't have the access to the code as it is weekend here. I will share the code tomorrow. Using yield return multipls times is freezing the app.just asking whether using async/await will help me or not
Yield return doesnt freeze the app, it does exactly the opposite thing, thr app gets frozen when it is waitibg to one method to end, using yield return "pauses" the method untill the next update cycle. they sre just asynchronous methods
Do you even read the comments? Why did you again post an answer? As i said in the comment above, if you want to add more details to your question you should edit your question.