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
2
Question by RdJ · Apr 15, 2011 at 07:23 PM · texturecoroutinetexture2dthreadsloadimage

Is it possible to split Texture2D.LoadImage into multiple parts?

Is it possible to use Texture2D in other thread than main thread? I need to load an image from disk, edit it using the Texture2D class, save it to disk and put it on a gameobject. I've so far managed to load, edit and save, but it freezes my game for around a second :(

I'm currently using coroutines to run the process, but it doesn't seem to help.

Is it possible to use the Texture2D class in other threads, or is there another way to make it go faster?


Edit:

I've now made the function quicker, using yields.

The bottlenecks are now: - converting jpg bytes to texture using the LoadImage function. - encoding texture to png using EncodeToPNG. I can change this one to an array, because I will only need it as texture.

The question is now, is it possible to split the LoadImage into parts, so I can put yields in between?

I also think applying the texture to a gameobject will cause freezing, but I'll see that later on.

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 Statement · Apr 15, 2011 at 07:42 PM

I don't know myself, since I haven't tried this. You should be able to make a small test yourself to verify this. However, Unity3D is generally not thread safe. I doubt you can manipulate a texture in use of the engine in another thread. It might work if it's not used at all, but that is not a guarantee. If it would work, it could been a fluke chance, or it may work in this version of unity but not in the next.

What you might be able to do is extract the data from a texture into an array, and save the array to disk in a separate thread. That should work.

It is safe to read contents threaded, as long no other thread make edits to the content being read. That is why you could safe guard yourself by copying the data into an array first, to make sure no other thread edit the texture. I assume the file write is the operation that blocks your execution, not actually getting the texture data from the texture.

If you are having bottlenecks reading the texture data, consider having two storage methods. When you edit the texture, edit an array representing the texture as well as updating the actual texture (if possible). That way you won't have to pull out the entire texture data each time you need to write it to disk since you already have an array that is up to date. You should still make a copy of this intermediate array since otherwise you might edit the array while its in use of a file write operation. You can use Array.Copy to efficiently copy an entire array.

Comment
Add comment · Show 9 · 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 RdJ · Apr 16, 2011 at 03:47 PM 0
Share

Check out the edit for my reaction.

avatar image Statement · Apr 16, 2011 at 04:13 PM 0
Share

Applying the texture to a material shouldn't cause much of a freeze - it's already loaded into memory - right? However the process of LoadImage and EncodeToPng is probably not going to be helped without use of threads or coroutines. Take a look at the WWW class which can load images with coroutines (yield). When you want to save the file, you could either use EncodeToPng to get the bytes and save the file async in a separate thread or by using coroutines with a binary writer to save a few bytes at a time, or, get the pixels with GetPixels and save a png or jpg file with some plugin.

avatar image Statement · Apr 16, 2011 at 04:16 PM 0
Share

Depending on your needs, you could make your own format which you can read/write with coroutines or threads, and use SetPixels and GetPixels and Apply to interface with the texture. If SetPixels etc take too long to process, you can set blocks of the texture in a coroutine. I think it would be easier using some image library to save to jpg/png though.

avatar image RdJ · Apr 16, 2011 at 05:12 PM 0
Share

I'm going to use GetPixel to get all pixels seperately, edit them if needed and save them to a text file. The EncodeToPng is really the biggest bottleneck and using this method I can get rid of it :).

For LoadImage however, I don't have an alternative (yet), as I have to convert a .jpg I download to a Texture2D.

avatar image Statement · Apr 16, 2011 at 05:29 PM 0
Share

Well, can't you just SetPixel/s from the text file? Or use WWW class (it handle local files too)

Show more comments

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

No one has followed this question yet.

Related Questions

Render a list of objects into a Texture2D array 0 Answers

Loading images fails on Android for existing Texture2Ds. 1 Answer

cant get Texture2D.LoadImage() to work 0 Answers

Array of arrays of Textures 2 Answers

Difficulty loading alpha textures at runtime 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