Texture2D - Get File size / Btyes (Runtime)

I have a texture2D coming in dynamically, and I need to get the size of it.

It comes in as a Texture2D. Originally I was using this to get the byte size:


But this only works within the Editor - I need to to run mainly on iOS. I just need to check how big it is before I commence any hard processing configurations on the texture2D … I don’t want to go ahead with it if the files are over a certain byte size.


Do you need the actual in-memory size? Taking compression into account?

  • If you’re looking for just the raw size it would just be something like width x height x (a value depending on the texture format, most likely 4).

    That would be the first mipmap level, then loop through Texture2D.mipmapCount, adding half the size (uh, not half, but whatever factor it’d be reduced by :slight_smile: for each level…

    I suppose there is potential in-memory padding and aligning and such that the GPU does, but does something like this get you close enough?

  • You can get a native pointer to the texture and potentially use a lower level graphics API call to get the size.

  • One more and it’s definitely hacky - call Texture2D.GetRawTextureData. The length of the returned byte array is the size. Don’t know for sure if that’s compressed data or not.

  • Last one. If you’re saving these textures to disk, you can use System.IO.FileInfo(path).Length to get the file size. I believe this will work on iOS but not 100% sure.