- Home /
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.
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.
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.
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.
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.
Well, can't you just SetPixel/s from the text file? Or use WWW class (it handle local files too)
Your answer
Follow this Question
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