Esempio n. 1
0
        public static string[] Export(AtlasRaw atlas, SpriteRaw[] sprites, string folder)
        {
            var exportSprites = new string[sprites.Length];
            var atlasSprites  = sprites.Select(i => PackAtlasSprite.ParseSprite(atlas.bins[i.bin], i)).ToArray();

            PackUtil.LoadAtlasTextures(atlasSprites);
            for (int i = 0; i < atlasSprites.Length; i++)
            {
                var sprite  = atlasSprites[i];
                var rect    = sprite.rect;
                var texture = PackUtil.CreateBlankTexture((int)rect.width, (int)rect.height, sprite.transparency ? TextureFormat.RGBA32 : TextureFormat.RGB24);
                texture.SetPixels(sprite.Read());
                var bytes = sprite.transparency ? texture.EncodeToPNG() : texture.EncodeToJPG();
                var path  = Path.Combine(folder, sprite.name) + (sprite.transparency ? ".png" : ".jpg");
                var dir   = Path.GetDirectoryName(path);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                File.WriteAllBytes(path, bytes);
                exportSprites[i] = path;
                if (EditorUtility.DisplayCancelableProgressBar("", "export: " + sprite.name, (float)i / sprites.Length))
                {
                    break;
                }
            }
            EditorUtility.ClearProgressBar();
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
            return(exportSprites);
        }
Esempio n. 2
0
        private void AddFiles(AtlasRaw atlas, PackQuality quality, string[] files, bool replaceSpriteByName = true)
        {
            var textures = new List <IPackSprite>(PackAtlasSprite.ListSprites(atlas));
            Action <IPackSprite> addTexture = (texture) =>
            {
                if (replaceSpriteByName)
                {
                    for (int i = textures.Count - 1; i >= 0; i--)
                    {
                        if (textures[i].name.Equals(texture.name))
                        {
                            textures.RemoveAt(i);
                        }
                    }
                }
                textures.Add(texture);
            };

            foreach (var file in files)
            {
                if (File.Exists(file))
                {
                    var texture = new PackAssetSprite(file);
                    texture.name    = Path.GetFileNameWithoutExtension(file);
                    texture.quality = quality;
                    addTexture(texture);
                }
                else if (Directory.Exists(file))
                {
                    var images = new List <string>();
                    images.AddRange(Directory.GetFiles(file, "*.png", SearchOption.AllDirectories));
                    images.AddRange(Directory.GetFiles(file, "*.jpg", SearchOption.AllDirectories));
                    foreach (var image in images)
                    {
                        var assetPath  = image.Replace(file + "\\", "").Replace("\\", "/");
                        var assetDir   = Path.GetDirectoryName(assetPath);
                        var assetName  = Path.GetFileNameWithoutExtension(assetPath);
                        var assetLabel = string.IsNullOrEmpty(assetDir) ? assetName : assetDir + "/" + assetName;
                        var texture    = new PackAssetSprite(image)
                        {
                            name    = assetLabel,
                            quality = quality
                        };
                        addTexture(texture);
                    }
                }
            }
            AtlasPacker.Repack(atlas, textures.ToArray());
        }
Esempio n. 3
0
        public static AtlasRaw Repack(AtlasRaw atlasRaw, IPackSprite[] textures = null, PackSetting setting = null)
        {
            EditorUtility.DisplayProgressBar("", "repack atlas...", 0.5f);
            textures = textures ?? PackAtlasSprite.ListSprites(atlasRaw);
            setting  = setting ?? new PackSetting(atlasRaw.maxSize, atlasRaw.padding, atlasRaw.isPOT, atlasRaw.forceSquare);
            var atlasPath = AssetDatabase.GetAssetPath(atlasRaw);

            atlasRaw = new PackAtlas(setting, atlasPath, textures).GenerateAtlas();
            EditorUtility.ClearProgressBar();
            if (atlasRaw == null)
            {
                Debug.LogError("Pack failed.");
                return(null);
            }
            RefreshUI();
            return(atlasRaw);
        }
Esempio n. 4
0
        private void DeleteSelected(AtlasRaw atlas, List <SpriteIndex> selected)
        {
            var list = new List <IPackSprite>(PackAtlasSprite.ListSprites(atlas));

            foreach (var selectedSprite in selected)
            {
                var bin    = atlas.bins[selectedSprite.bin];
                var sprite = bin.sprites[selectedSprite.sprite];
                for (int i = list.Count - 1; i >= 0; i--)
                {
                    if (string.Equals(list[i].name, sprite.name))
                    {
                        list.RemoveAt(i);
                    }
                }
            }
            AtlasPacker.Repack(atlas, list.ToArray());
        }
Esempio n. 5
0
        private void CompressSelected(AtlasRaw atlas, PackQuality quality, List <SpriteIndex> selected)
        {
            var count = 0;
            var list  = new List <IPackSprite>(PackAtlasSprite.ListSprites(atlas));

            foreach (var selectedSprite in selected)
            {
                var bin    = atlas.bins[selectedSprite.bin];
                var sprite = bin.sprites[selectedSprite.sprite];
                foreach (var packSprite in list)
                {
                    if (string.Equals(packSprite.name, sprite.name) &&
                        packSprite.quality != quality)
                    {
                        packSprite.quality = quality;
                        count += 1;
                    }
                }
            }
            if (count > 0)
            {
                AtlasPacker.Repack(atlas, list.ToArray());
            }
        }