Unity中如何计算带minimap的贴图资源的大小
生活随笔
收集整理的這篇文章主要介紹了
Unity中如何计算带minimap的贴图资源的大小
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/// <summary>/// 計(jì)算貼圖大小,包含mipmap內(nèi)存的疊加/// </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>/// 計(jì)算貼圖大小,包含mipmap內(nèi)存的疊加,指定貼圖格式/// </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("------------------>有貼圖格式未設(shè)置: 貼圖名稱:" + 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>/// 獲取對(duì)應(yīng)個(gè)是貼圖的位大小/// </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 formatreturn 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 formatreturn 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 camerareturn 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 formatreturn 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 formatreturn 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:// 沒有設(shè)置貼圖格式,默認(rèn)給4bit.return 4;}return 0;}
?
轉(zhuǎn)載于:https://www.cnblogs.com/hengsoft/p/10289647.html
總結(jié)
以上是生活随笔為你收集整理的Unity中如何计算带minimap的贴图资源的大小的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决Ubuntu16.04重启进入ini
- 下一篇: Luogu4606 SDOI2018 战