- Home /
Fastest way to load a Texture2D in Unity (web player)?
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?
Im no expert, but have you tried loading it into the scene, on an object off cam at start of game, and then when you need to use it it will already be there in memory?
Only way i can think of doing it, otherwise, like you said, it will pause...
Hey @$$anonymous$$larax,
Your solution would certainly work in a lot of cases. A similar one would be to include your texture in an asset bundle, though I'm not sure if there's any delay in loading textures from bundles.
Unfortunately, in our case we don't know beforehand what textures we'll need for a given user. We're actually working on a data visualization project using Unity, which is a bit of a different use case from many users.
Thanks for the idea!
Your answer
Follow this Question
Related Questions
issue with create texture2d in background Task, probably a bug ? 0 Answers
Can I create a sprite at runtime? 3 Answers
How to show image on Camera View programmatically in Unity3d 1 Answer
How does setting the hideFlags resolve leaking issues? (Texture 2D leaking) 2 Answers
rotate an image by modifying Texture2D.GetPixels32() array 2 Answers