Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by peterworth · Jul 24, 2014 at 12:19 PM · c#wwwyieldthreads

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.

Comment
Add comment · Show 5
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 DajBuzi · Jul 24, 2014 at 12:51 PM 1
Share

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.

avatar image peterworth · Aug 05, 2014 at 07:41 AM 0
Share

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?

avatar image peterworth · Aug 05, 2014 at 12:31 PM 0
Share

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.

avatar image VesuvianPrime · Aug 05, 2014 at 12:37 PM 0
Share

$$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.

avatar image WilliamLeu · Aug 05, 2014 at 04:15 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

25 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

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


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