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
1
Question by Clavus · Jan 13, 2015 at 02:18 AM · c#threadingloading textures

Loading a large external image to texture without freezing the game?

I'm building an application with Unity that loads large PNG images from a nearby folder. To do this I use the WWW class (http://docs.unity3d.com/ScriptReference/WWW.html). After that I load the image file into a texture, which I assign to a Sprite as such:

 Texture2D tex = new Texture2D(4, 4, TextureFormat.ARGB32, true);
 wwwObject.LoadImageIntoTexture(tex);
 
 Sprite spr = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0), 100);


Uses http://docs.unity3d.com/ScriptReference/WWW.LoadImageIntoTexture.html

All this works nice and dandy but it freezes the game's main thread while it's presumably loading and then compressing PNG files with resolutions spanning from 1080p to 4K. I've tried to look for ways to do this in a background thread or something but the Unity API is thread-unsafe and very pissy about that. Is there a way to load these images without freezing my game?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Jan 13, 2015 at 02:26 AM

I believe the WWW class is already creating a Texture2D for you, so you should be able to just use the wwwObject.texture (or wwwObject.textureNonReadable) variable instead of creating a new Texture2D, unless as the documentation states that you need to download a different texture continuously. Check out http://docs.unity3d.com/ScriptReference/WWW-texture.html

Another thing to keep in mind is that when you call LoadImageIntoTexture, it's going to resize the texture you pass in every time, since you are defining it as 4x4. It is more optimized to be able to replace a texture of the same size as the data, so that could be part of your slow down.

Alternatively, if the wwwObject.texture variable doesn't work for you for some reason, you can create your own coroutine, and load the data from the wwwObject manually, say 1 row at a time, each frame, until the Texture2D is fully copied, then have some callback at the end of the coroutine to set the texture. During this time, you could set a progress indication.

EDIT: Also note that you should use the WWW.textureNonReadable variable if you don't need to read pixel data from the texture.

Comment
Add comment · Show 5 · 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 Clavus · Jan 13, 2015 at 10:50 AM 0
Share

Wonder why I missed the fact www objects already made their own Texture2D. However they game still freezes when I try to read the wwwObject.texture / .textureNonReadable. I timed it as such:

 System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
 stopwatch.Start();
 
 Texture2D tex = file.textureNonReadable;
 stopwatch.Stop();
 Debug.Log("Time to load: " + stopwatch.Elapsed$$anonymous$$illiseconds + "ms");

The various images I have take from 50ms to even 360ms in this piece of code. So my next question is, do you have an example of loading a Texture2D from the wwwObject through a coroutine?

avatar image supernat · Jan 15, 2015 at 05:57 AM 0
Share

Unfortunately, it's very complicated because you have to implement the image decompression algorithms on the raw image data you downloaded (wwwObject.bytes). But the idea would be to use those bytes and decompress a portion of the image into the Texture2D each frame while yielding, until the whole image is in the Texture2D and it is ready to use. In theory, you could maybe find a jpg/png decompressor library on the asset store (I haven't looked), and you could decompress the raw bytes in a separate thread into a new buffer of just bytes, then copy the entire contents of the new buffer into the Texture2D once the decompression is complete (I'm guessing the decompression is the part taking the longest).

avatar image Clavus · Jan 16, 2015 at 02:07 AM 0
Share

Hmm, ok, so what I guess the workflow would look like: create www object, pass it to a separate thread, read bytes and decompress to raw image data (without using Unity API). Pass it back to the main thread, create a Texture2D of the same size as the original image, use Texture2D.SetPixels function in coroutine to gradually build up the texture. Correct?

avatar image hexagonius Clavus · Dec 17, 2018 at 11:07 AM 0
Share

@Clavus Ok this is old, but still a thing even in Unity 2018... Did anyone do this?

avatar image Clavus hexagonius · Dec 17, 2018 at 12:22 PM 0
Share

I think nowadays this is the solution: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestTexture.GetTexture.html

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Delegates and "__ Can only be called from main thread" 2 Answers

Stalling Integration ThreadAwake not completed error 0 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