I’ve got some PNG byte data in my Resources folder that I want to load at runtime on Android and iOS devices. The image file itself is 2048x4096, which equates to an uncompressed RGBA 32-bit image at 32MB, according to the Texture Import Settings.
So, I take my 32MB texture, give it a .bytes extension, and throw it into the Resources folder. Then, at runtime on an Android device, I call this code to load the bytes into a texture:
TextAsset imageBytes = Resources.Load( atlasPath, typeof( TextAsset ) ) as TextAsset; texture = new Texture2D( 0, 0, TextureFormat.ARGB32, false ); texture.LoadImage( imageBytes.bytes ); texture.name = atlasPath; Resources.UnloadAsset( imageBytes );
This works well, but we come to my question: is this the best way to load a texture dynamically at runtime to achieve the best performance/memory usage?
Specifically, I’ve noticed that when I profile memory usage at runtime, my originally 32MB texture is now 64MB in memory! For some reason, the texture size has doubled. Is this a side effect of using the “LoadImage” function? Or does it have something to do with the image being “Read/Write Enabled”? Is there anyway to avoid that sort of memory bloat when loading a texture dynamically like this?