Non-blocking construction (loading and copying) of large Texture2D's in C#

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

Eventually this problem was solved by creating a C++ Plugin (built through Android Studio 2.2) that makes use of “stb_image.h” for loading the image, and OpenGL to generate textures and map a set of scanlines onto the texture over multiple frames. The texture is then handed over to Unity through Texture2D.CreateExternalTexture().

This method does not make the work asynchronous but spreads the loading cost over multiple frames removing the synchronous block and subsequent frame out.

I wasn’t able to make the texture creation asynchronous because in order for the OpenGL functions to work you are required to be running the code from Unity’s main Render Thread, so functions must be called through GL.IssuePluginEvent() - Unity’s docs use the following project to explain how to make use of this functionality: https://bitbucket.org/Unity-Technologies/graphicsdemos/

I’ve cleaned up the test repo I was working on and written instructions in the README to make it as easy as possible to understand the final solution I came to. I hope that it will be of use to someone at some point and that they won’t have to spend as long as I’ve done to solve this problem! https://github.com/NeoSouldier/Texture2DTest/