public static void FindRGBA() { TextureImporterFormat[] formatConfig = new TextureImporterFormat[] { TextureImporterFormat.RGBA32, TextureImporterFormat.RGB24, TextureImporterFormat.ARGB32, }; HashSet <TextureImporterFormat> filter = new HashSet <TextureImporterFormat>(formatConfig); foreach (string atlasFile in EditorResourcePaths.GetAllAtlasFiles()) { if (atlasFile.EndsWith(LOW_ATLAS_SUFFIX)) { continue; } SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasFile); for (int i = 2; i < PLATFORMS.Length; i++) { TextureImporterPlatformSettings patformSettings = SpriteAtlasExtensions.GetPlatformSettings(spriteAtlas, PLATFORMS[i]); if (patformSettings.overridden && filter.Contains(patformSettings.format)) { DebugUtil.LogWarning("高清图集 : " + atlasFile); break; } } } DebugUtil.Log("FindRGBA Done!"); }
static void HandleSpriteAtlasFormat(HashSet <string> atlasFiles) { string[] atlasFileArray = new string[ATLAS_TYPE_NUMBER]; SpriteAtlas[] spriteAtlasArray = new SpriteAtlas[ATLAS_TYPE_NUMBER]; SpriteAtlasPackingSettings[] packingSettingsArray = new SpriteAtlasPackingSettings[ATLAS_TYPE_NUMBER]; SpriteAtlasTextureSettings[] textureSettingsArray = new SpriteAtlasTextureSettings[ATLAS_TYPE_NUMBER]; Dictionary <string, TextureImporterPlatformSettings>[] platformSettingsDictArray = new Dictionary <string, TextureImporterPlatformSettings> [ATLAS_TYPE_NUMBER]; platformSettingsDictArray[0] = new Dictionary <string, TextureImporterPlatformSettings>(); platformSettingsDictArray[1] = new Dictionary <string, TextureImporterPlatformSettings>(); foreach (string file in atlasFiles) { if (file.EndsWith(LOW_ATLAS_SUFFIX)) { continue; } atlasFileArray[0] = file; atlasFileArray[1] = file.Replace(EditorResourcePaths.ATLAS_SUFFIX, LOW_ATLAS_SUFFIX); for (int i = 0; i < ATLAS_TYPE_NUMBER; i++) { spriteAtlasArray[i] = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasFileArray[i]); packingSettingsArray[i] = SpriteAtlasExtensions.GetPackingSettings(spriteAtlasArray[i]); textureSettingsArray[i] = SpriteAtlasExtensions.GetTextureSettings(spriteAtlasArray[i]); foreach (string platform in PLATFORMS) { platformSettingsDictArray[i][platform] = SpriteAtlasExtensions.GetPlatformSettings(spriteAtlasArray[i], platform); } } HashSet <string> spriteSet = EditorResourcePaths.GetDependencies(atlasFileArray, EditorResourcePaths.PNG_REGEX); bool containAlpha = false; foreach (string spriteFile in spriteSet) { Texture2D texture = EditorCommonUtils.ReadTexture(spriteFile); if (ContainAlpha(texture)) { containAlpha = true; break; } } for (int i = 0; i < ATLAS_TYPE_NUMBER; i++) { packingSettingsArray[i].enableRotation = false; packingSettingsArray[i].enableTightPacking = false; SetDefaultTextureSettings(ref textureSettingsArray[i]); } if (containAlpha) { SetPlatformTextureSettings(platformSettingsDictArray[0]["Default"], "Default", TextureImporterFormat.RGBA32); SetPlatformTextureSettings(platformSettingsDictArray[0]["Standalone"], "Standalone", TextureImporterFormat.DXT5); SetPlatformTextureSettings(platformSettingsDictArray[0]["Android"], "Android", TextureImporterFormat.ETC2_RGBA8); SetPlatformTextureSettings(platformSettingsDictArray[0]["iPhone"], "iPhone", IOS_NEW_RGBA); SetPlatformTextureSettings(platformSettingsDictArray[1]["Default"], "Default", TextureImporterFormat.RGBA32); SetPlatformTextureSettings(platformSettingsDictArray[1]["Standalone"], "Standalone", TextureImporterFormat.DXT5Crunched); SetPlatformTextureSettings(platformSettingsDictArray[1]["Android"], "Android", TextureImporterFormat.ETC2_RGBA8Crunched); SetPlatformTextureSettings(platformSettingsDictArray[1]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGBA4); } else { SetPlatformTextureSettings(platformSettingsDictArray[0]["Default"], "Default", TextureImporterFormat.RGB24); SetPlatformTextureSettings(platformSettingsDictArray[0]["Standalone"], "Standalone", TextureImporterFormat.DXT1); SetPlatformTextureSettings(platformSettingsDictArray[0]["Android"], "Android", TextureImporterFormat.ETC2_RGB4); SetPlatformTextureSettings(platformSettingsDictArray[0]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGB4); SetPlatformTextureSettings(platformSettingsDictArray[1]["Default"], "Default", TextureImporterFormat.RGB24); SetPlatformTextureSettings(platformSettingsDictArray[1]["Standalone"], "Standalone", TextureImporterFormat.DXT1Crunched); SetPlatformTextureSettings(platformSettingsDictArray[1]["Android"], "Android", TextureImporterFormat.ETC_RGB4Crunched); SetPlatformTextureSettings(platformSettingsDictArray[1]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGB4); } for (int i = 0; i < ATLAS_TYPE_NUMBER; i++) { SpriteAtlasExtensions.SetIncludeInBuild(spriteAtlasArray[i], false); SpriteAtlasExtensions.SetPackingSettings(spriteAtlasArray[i], packingSettingsArray[i]); SpriteAtlasExtensions.SetTextureSettings(spriteAtlasArray[i], textureSettingsArray[i]); foreach (string platform in PLATFORMS) { SpriteAtlasExtensions.SetPlatformSettings(spriteAtlasArray[i], platformSettingsDictArray[i][platform]); } } SpriteAtlasExtensions.SetIsVariant(spriteAtlasArray[1], false); SpriteAtlasExtensions.Remove(spriteAtlasArray[1], SpriteAtlasExtensions.GetPackables(spriteAtlasArray[1])); SpriteAtlasExtensions.Add(spriteAtlasArray[1], SpriteAtlasExtensions.GetPackables(spriteAtlasArray[0])); } AssetDatabase.Refresh(); }
public static void SetAtlasMaxSize() { MethodInfo getPreviewTexturesMI = typeof(SpriteAtlasExtensions).GetMethod("GetPreviewTextures", BindingFlags.Static | BindingFlags.NonPublic); foreach (string atlasFile in EditorResourcePaths.GetAllAtlasFiles()) { if (atlasFile.EndsWith(LOW_ATLAS_SUFFIX)) { continue; } SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasFile); List <Sprite> sprites = GetSpritesByAtlas(spriteAtlas); // 统计散图面积之和 int total = 0; int minSize = 128; foreach (Sprite sprite in sprites) { Rect rect = sprite.textureRect; int width = Mathf.CeilToInt(rect.width); int height = Mathf.CeilToInt(rect.height); minSize = Mathf.Max(minSize, width, height); total += width * height; } // 估算maxTextureSize值 float guessArea = EditorCommonUtils.ToLargerPOT(total, 4); int guessSize = Mathf.RoundToInt(Mathf.Sqrt(guessArea)); // 如果填充率太低,将估值尺寸减半 if (total <= guessArea * 0.625f) { guessSize /= 2; } // 用估值尺寸尝试打图集 guessSize = Mathf.Max(guessSize, Mathf.RoundToInt(EditorCommonUtils.ToLargerPOT(minSize, 2))); foreach (string platform in PLATFORMS) { TextureImporterPlatformSettings patformSettings = SpriteAtlasExtensions.GetPlatformSettings(spriteAtlas, platform); if (patformSettings.maxTextureSize != guessSize) { patformSettings.maxTextureSize = guessSize; SpriteAtlasExtensions.SetPlatformSettings(spriteAtlas, patformSettings); } } SpriteAtlasUtility.PackAtlases(new SpriteAtlas[] { spriteAtlas }, EditorUserBuildSettings.activeBuildTarget); // 计算图集的实际面积 Texture2D[] atlasTextures = (Texture2D[])getPreviewTexturesMI.Invoke(null, new object[] { spriteAtlas }); int area = 0; for (int i = 0; i < atlasTextures.Length; i++) { area += atlasTextures[i].width * atlasTextures[i].height; } // 如果估值尺寸效果一般,将其翻倍 if (area > guessSize * guessSize * 2.5f) { foreach (string platform in PLATFORMS) { TextureImporterPlatformSettings patformSettings = SpriteAtlasExtensions.GetPlatformSettings(spriteAtlas, platform); patformSettings.maxTextureSize = Mathf.Min(guessSize * 2, MAX_TEXTURE_SIZE); SpriteAtlasExtensions.SetPlatformSettings(spriteAtlas, patformSettings); } SpriteAtlasUtility.PackAtlases(new SpriteAtlas[] { spriteAtlas }, EditorUserBuildSettings.activeBuildTarget); } else if (area > guessSize * guessSize * 1.5f && area <= guessSize * guessSize * 2) { TextureImporterPlatformSettings patformSettings = SpriteAtlasExtensions.GetPlatformSettings(spriteAtlas, "Android"); patformSettings.maxTextureSize = Mathf.Min(guessSize * 2, MAX_TEXTURE_SIZE); SpriteAtlasExtensions.SetPlatformSettings(spriteAtlas, patformSettings); SpriteAtlasUtility.PackAtlases(new SpriteAtlas[] { spriteAtlas }, EditorUserBuildSettings.activeBuildTarget); } SpriteAtlas lowSpriteAtlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasFile.Replace(EditorResourcePaths.ATLAS_SUFFIX, LOW_ATLAS_SUFFIX)); foreach (string platform in PLATFORMS) { TextureImporterPlatformSettings patformSettings = SpriteAtlasExtensions.GetPlatformSettings(spriteAtlas, platform); TextureImporterPlatformSettings lowPatformSettings = SpriteAtlasExtensions.GetPlatformSettings(lowSpriteAtlas, platform); lowPatformSettings.maxTextureSize = patformSettings.maxTextureSize; SpriteAtlasExtensions.SetPlatformSettings(lowSpriteAtlas, lowPatformSettings); } } DebugUtil.Log("SetAtlasMaxSize Done!"); }