- Home /
byte[] to Texture kills performance
I added the functionality to download images and make a Skybox out of it. With a loop within a Coroutine, it works fine. But at the steps
byte[] = bytes = null;
bytes = www.texture.EncodeToPNG();
Texture2D tx = new Texture2D(2, 2);
tx.LoadImage(bytes);
cost much of time and the app laggs.
Any ideas to prevent that?
Answer by fmielke · Feb 21, 2017 at 12:47 AM
WWW.LoadImageIntoTexture
instead of the code above solved the problem.
Answer by Just_John · Feb 21, 2017 at 08:15 AM
Are you actually connecting to a website and trying to render images from it? It could be limited by connection speed to some degree. Maybe try saving the image locally and connecting to that and see if it improves performance at all?
Answer by MarshallN · Feb 21, 2017 at 12:06 AM
Unfortunately, you're in a bit of a bind - the Texture2D.EncodeToPNG() function is inbuilt to Unity which means you can't run it on its own thread, so your best bet would be to write your own coroutine that mimics its function, or else use a third party image processing library (there are probably examples on the Asset store). It's also dumping a massive amount - 3.5 MB - into garbage collection, but I don't know if there's any way to avoid that.
How important is it that this script run repeatedly? If it's something that only runs when the user selects an image to use as their skybox, a 0.3-second performance hit isn't awful.
Alternatively, passing the colors through over time on a coroutine might be more efficient, then building the image once all colors are passed through the network.
Can you explain this more detailed, please? A second Coroutine?