Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by zulqarnain26 · Apr 04, 2019 at 07:44 AM · coroutinecoroutinesthreadsthreadingcoroutine errors

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??

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Apr 04, 2019 at 07:52 AM 0
Share

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?

avatar image zulqarnain26 · Apr 05, 2019 at 07:35 AM 0
Share

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 ?

avatar image xxmariofer zulqarnain26 · Apr 05, 2019 at 07:41 AM -1
Share

what do you mean by load? download from somewhere? can you share your code so we can check where you could optimize it?

avatar image Bunny83 zulqarnain26 · Apr 05, 2019 at 11:15 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-2

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Apr 07, 2019 at 07:01 PM 0
Share

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

avatar image zulqarnain26 xxmariofer · Apr 07, 2019 at 08:18 PM 0
Share

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

avatar image xxmariofer zulqarnain26 · Apr 07, 2019 at 09:09 PM 0
Share

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

avatar image Bunny83 · Apr 07, 2019 at 11:38 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Return value from coroutine to non monobehaviour 1 Answer

Had difficulties implementing intro to Coroutines from unitypatterns.com. Help? 1 Answer

What's the best way to structure a recursive long running algorithm? 1 Answer

StopAllCoroutines not working 0 Answers

Threading in Unity 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges