I’m building a VR app for Android which deals with loading a lot of large textures dynamically (all images are over 6MB in size as png’s). These textures can either come from an Amazon S3 server, in which case they arrive as a stream, or from the user’s device itself.
In both cases I’m able to get hold of the raw data or texture asynchronously without a problem. In the first I query the server and get a callback with the stream of data, and in the second I use the WWW class to get hold of the texture making use of the “file://” protocol.
The problem happens as soon as I want to actually construct a Texture2D from this data so that I can make use of it.
With the stream I convert it into a byte and try calling LoadImage(), and with the WWW class I simply try copying it with myTexture = www.texture. Both times I get a massive frame out as the Texture2D object is created. I want to eradicate this frame out because the app is simply un-shippable with it.
using (var stream = responseStream)
byte myBinary = ToByteArray(stream);
m_myTexture.LoadImage(myBinary); // Commenting this line removes frame out
WWW www = new WWW("file://" + filePath);
yield return www;
m_myTexture = www.texture; // Commenting this line removes frame out
Unfortunately Unity doesn’t seem to like running these operations on a separate thread from the main thread and throws an exception when I try, saying that construction can only be done on the main thread.
Is there some way to perform construction operation on a seperate thread anyway? Or perhaps chunk up construction, so that I can spread it over multiple frames? Or maybe do some sort of fast memcopy operation that won’t stall the main thread?
Thanks in advance!
PS: I asked pretty much the exact same question on Stack Overflow earlier this month, and have yet to get a working answer: goo.gl/UfSOtT
PPS: I’ve created a working example of the problem in the following repo: GitHub - arthur-fox/Texture2DTest: Test repo demonstrating the synchronous block on the main thread in Unity, when constructing a large Texture2D