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.

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.