GUITexture problems

Hello,

We’re having a problem with uncompressed textures on the iPhone.

GUITextures with uncompressed, non-power of 2 textures assigned to their texture property seem to take up more memory than the size of the texture would indicate. In one test I ran, the asset’s size was approximately 100Kb, but appeared to use more than 1MB of memory when I ran tests from within Instruments with and without the texture.

Since our GUITextures are primarily used within menus such as “Pause” and “Start” screens, our strategy is to instantiate the GUITexture’s texture dynamically, then destroy it once the menu has been dismissed. What is the best practice for doing so?

So far we’ve written the following functions:

private void CreateTexture(GUITexture guiTexture, string texturePath)
{
	guiTexture.texture = (Texture) Resources.Load(texturePath);

	// Can't explicitly destroy textures, hope garbage collection 
        //removes any previously assigned texture.

	Application.GarbageCollectUnusedAssets();
}

private void DestroyTexture(GUITexture guiTexture)
{
	// We're not allowed to set the texture property to null, 
        //so assign a small dummy texture to the guiTexture, then hope 		
        // garbage collection collects the GIANT texture we're trying to delete!
	guiTexture.texture = new Texture2D(4, 4);
	Application.GarbageCollectUnusedAssets();
}

I’ve ran a few tests through Instruments, and memory rises/falls as expected when a menu is called/dismissed.

Are we on the right track here in terms of memory management? Is there a better way to do it?

Marc``

the size of a texture in memory, when it does not qualify for pvr compression and you don’t use 16bit, is always length * height * 4 byte so the size of a plain bmp.

Also, textures are always expanded to the next power of 2 size, so a 480x320 image would actually become a 512x512 texture, which is 1.5mb in size with mipmaps.

100kb sounds more like the size of a jpg / png which will become regular byte arrays in memory (-> bmp)

iPhone GPU has no support for non-power-of-2 textures. So Unity has to change your texture to power-of-2 at some point.

If you have a non-power-of-2 texture which was not scaled in the Editor, then at run-time actually 2 more textures will have to be created by Unity in memory! One texture will be scaled to the next power-of-two, another will be padded to the next power-of-two. Padded texture is required for GUITexture and scaled one can be used with ordinary geometry.