1. 程式人生 > >Unity中如何計算帶minimap的貼圖資源的大小

Unity中如何計算帶minimap的貼圖資源的大小

format res text tom sum summary returns ati 獲取


///
<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; }

Unity中如何計算帶minimap的貼圖資源的大小