I'm trying to figure out the fastest way to create or update a Texture2D in Unity.
The fastest way by far is to use unmanaged code - a native plugin - that directly overwrites the texture in memory without going through Unity. Unfortunately, we can't use unmanaged code in the Unity web player, so I'm trying to determine just how fast we can load a texture using the methods Unity has offered us.
The reason for this is that Unity loads new textures on its main thread. This means that the entire Unity application waits while a texture is being created or loaded, and if you try to load a large texture (say 4096x4096) it will actually noticeably freeze the application while this is going on. Definitely not a good thing.
We played around with the basic ways of creating/updating textures a while back. Both SetPixels32 and LoadImage are just too slow, no matter what tweaks we tried.
Recently, we tried loading raw data through Texture2D.LoadRawTextureData and passing ARGB data. For 4096x2048 it was taking 20 ms - too slow. The time seemed to be linear - a 2048x2048 image took 10 ms, and loading just RGB took 15ms. We can also try directly loading DXT data - perhaps it too will linearly scale, and the 4x reduction will make it fast enough. Of course, compression doesn't work for every use case.
Does anyone else know a faster way of creating/loading to a texture in Unity, within the restrictions of the web player?
↧