Unity中如何计算带minimap的贴图资源的大小


///
<summary> /// 计算贴图大小,包含mipmap内存的叠加 /// </summary> /// <param name="tTexture"></param> /// <returns></returns> public static int CalculateTextureSizeBytes(Texture tTexture) { int tWidth = tTexture.width; int tHeight = tTexture.height; if (tTexture is Texture2D) { Texture2D tTex2D = tTexture as Texture2D; int bitsPerPixel = GetBitsPerPixel(tTex2D.format); int mipMapCount = tTex2D.mipmapCount; int mipLevel = 1; int tSize = 0; while (mipLevel <= mipMapCount) { tSize += tWidth * tHeight * bitsPerPixel / 8; tWidth = tWidth / 2; tHeight = tHeight / 2; mipLevel++; } return tSize; } return 0; } /// <summary> /// 计算贴图大小,包含mipmap内存的叠加,指定贴图格式 /// </summary> /// <param name="tTexture"></param> /// <returns></returns> public static int CalculateTextureSizeBytesByFormat(Texture tTexture, TextureImporterFormat format) { int tWidth = tTexture.width; int tHeight = tTexture.height; if (tTexture is Texture2D) { Texture2D tTex2D = tTexture as Texture2D; if (TextureImporterFormat.Automatic == format) { Debug.LogError("------------------>有贴图格式未设置: 贴图名称:" + tTexture.name); } int bitsPerPixel = GetBitsPerPixelForImportFormat(format); int mipMapCount = tTex2D.mipmapCount; int mipLevel = 1; int tSize = 0; while (mipLevel <= mipMapCount) { tSize += tWidth * tHeight * bitsPerPixel / 8; tWidth = tWidth / 2; tHeight = tHeight / 2; mipLevel++; } return tSize; } return 0; } /// <summary> /// 获取对应个是贴图的位大小 /// </summary> /// <param name="format"></param> /// <returns></returns> public static int GetBitsPerPixel(TextureFormat format) { switch (format) { case TextureFormat.Alpha8: // Alpha-only texture format. return 8; case TextureFormat.ARGB4444: // A 16 bits/pixel texture format. Texture stores color with an alpha channel. return 16; case TextureFormat.RGBA4444: // A 16 bits/pixel texture format. return 16; case TextureFormat.RGB24: // A color texture format. return 24; case TextureFormat.RGBA32: //Color with an alpha channel texture format. return 32; case TextureFormat.ARGB32: //Color with an alpha channel texture format. return 32; case TextureFormat.RGB565: // A 16 bit color texture format. return 16; case TextureFormat.DXT1: // Compressed color texture format. return 4; case TextureFormat.DXT5: // Compressed color with alpha channel texture format. return 8; case TextureFormat.PVRTC_RGB2:// PowerVR (iOS) 2 bits/pixel compressed color texture format. return 2; case TextureFormat.PVRTC_RGBA2:// PowerVR (iOS) 2 bits/pixel compressed with alpha channel texture format return 2; case TextureFormat.PVRTC_RGB4:// PowerVR (iOS) 4 bits/pixel compressed color texture format. return 4; case TextureFormat.PVRTC_RGBA4:// PowerVR (iOS) 4 bits/pixel compressed with alpha channel texture format return 4; case TextureFormat.ETC_RGB4:// ETC (GLES2.0) 4 bits/pixel compressed RGB texture format. return 4; case TextureFormat.ETC2_RGBA8:// ATC (ATITC) 8 bits/pixel compressed RGB texture format. return 8; case TextureFormat.BGRA32:// Format returned by iPhone camera return 32; } return 0; } public static int GetBitsPerPixelForImportFormat(TextureImporterFormat format) { switch (format) { case TextureImporterFormat.Alpha8: // Alpha-only texture format. return 8; case TextureImporterFormat.RGB24: // A color texture format. return 24; case TextureImporterFormat.RGBA32: //Color with an alpha channel texture format. return 32; case TextureImporterFormat.ARGB32: //Color with an alpha channel texture format. return 32; case TextureImporterFormat.RGBA16: // A 16 bit color texture format. return 16; case TextureImporterFormat.RGB16: // A 16 bit color texture format. return 16; case TextureImporterFormat.DXT1: // Compressed color texture format. return 4; case TextureImporterFormat.DXT5: // Compressed color with alpha channel texture format. return 8; case TextureImporterFormat.PVRTC_RGB2:// PowerVR (iOS) 2 bits/pixel compressed color texture format. return 2; case TextureImporterFormat.PVRTC_RGBA2:// PowerVR (iOS) 2 bits/pixel compressed with alpha channel texture format return 2; case TextureImporterFormat.PVRTC_RGB4:// PowerVR (iOS) 4 bits/pixel compressed color texture format. return 4; case TextureImporterFormat.PVRTC_RGBA4:// PowerVR (iOS) 4 bits/pixel compressed with alpha channel texture format return 4; case TextureImporterFormat.ETC_RGB4:// ETC (GLES2.0) 4 bits/pixel compressed RGB texture format. return 4; case TextureImporterFormat.ETC2_RGB4:// ETC (GLES3.0) 4 bits/pixel compressed RGB texture format. return 4; case TextureImporterFormat.ETC2_RGBA8:// ETC (GLES3.0) 8 bits/pixel compressed RGBA texture format. return 8; case TextureImporterFormat.Automatic:// 没有设置贴图格式,默认给4bit. return 4; } return 0; }

猜你喜欢

转载自www.cnblogs.com/hengsoft/p/10289647.html