Exemple #1
0
        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!");
        }
Exemple #2
0
        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!");
        }