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()); }
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); }
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()); }
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()); } }