Example #1
0
 public Sprite(AtlasRaw atlasRaw, string spriteName)
 {
     m_Type       = Type.Atlas;
     m_AtlasRaw   = atlasRaw;
     m_SpriteName = spriteName;
     Debug.Assert(!IsNull());
 }
Example #2
0
        private void RemoveAtlasTextures(AtlasRaw atlasRaw)
        {
            var texAssets = new List <Texture2D>();

            foreach (var bin in atlasRaw.bins)
            {
                if (bin.main != null)
                {
                    texAssets.Add(bin.main);
                }
                if (bin.addition != null)
                {
                    texAssets.Add(bin.addition);
                }
            }
            foreach (var texAsset in texAssets)
            {
                var texPath = AssetDatabase.GetAssetPath(texAsset);
                if (!string.IsNullOrEmpty(texPath))
                {
                    File.Delete(texPath);
                    var metaPath = texPath + ".meta";
                    if (File.Exists(metaPath))
                    {
                        File.Delete(metaPath);
                    }
                }
            }
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
        }
Example #3
0
 public void SetInitSprite(AtlasRaw atlasRaw, string spriteName)
 {
     mWords = "";
     mCache.Match(mWords, out mSprites, out mAtlases);
     mCache.Fetch(atlasRaw, spriteName, out mSelectedSprite);
     FilterAtlas(mSelectedSprite != null ? mSelectedSprite.atlas : -1);
 }
Example #4
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);
        }
Example #5
0
        private void DisplayImportMenu(AtlasRaw atlas, bool isFolder)
        {
            var menu         = new GenericMenu();
            var qualityNames = Enum.GetNames(typeof(PackQuality));

            foreach (var qualityName in qualityNames)
            {
                var quality = (PackQuality)Enum.Parse(typeof(PackQuality), qualityName);
                menu.AddItem(new GUIContent(qualityName), false, () =>
                {
                    string[] files = null;
                    if (isFolder)
                    {
                        files = SFB.StandaloneFileBrowser.OpenFolderPanel("Select Folder", "", true);
                    }
                    else
                    {
                        files = SFB.StandaloneFileBrowser.OpenFilePanel("Select File", "", new[] { new SFB.ExtensionFilter("Image Files", "png", "jpg") }, true);
                    }
                    if (files != null && files.Length > 0)
                    {
                        AddFiles(atlas, quality, files);
                    }
                });
            }
            menu.ShowAsContext();
        }
Example #6
0
        private void CreateAtlasTextures(AtlasRaw atlasRaw)
        {
            var atlasFolder = Path.GetDirectoryName(atlasPath);
            var atlasName   = Path.GetFileNameWithoutExtension(atlasPath);
            var bins        = atlasRaw.bins;

            for (int i = 0; i < bins.Length; i++)
            {
                var binRaw    = bins[i];
                var mainBytes = binRaw.main.EncodeToPNG();
                var mainPath  = Path.Combine(atlasFolder, atlasName + "_" + i + ".png");
                File.WriteAllBytes(mainPath, mainBytes);
                AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
                UnityEngine.Object.DestroyImmediate(binRaw.main);
                binRaw.main = AssetDatabase.LoadAssetAtPath <Texture2D>(mainPath);
                if (binRaw.addition != null)
                {
                    var addBytes = binRaw.addition.EncodeToPNG();
                    var addPath  = Path.Combine(atlasFolder, atlasName + "_" + i + "_add.png");
                    File.WriteAllBytes(addPath, addBytes);
                    AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
                    UnityEngine.Object.DestroyImmediate(binRaw.addition);
                    binRaw.addition = AssetDatabase.LoadAssetAtPath <Texture2D>(addPath);
                }
                var pp = PackPostProcessor.Fetch((PackQuality)binRaw.quality);
                pp.Setup(setting, binRaw);
                pp.Execute();
            }
        }
Example #7
0
        private void DisplayOperateMenu(AtlasRaw atlas)
        {
            var menu         = new GenericMenu();
            var qualityNames = Enum.GetNames(typeof(PackQuality));

            foreach (var qualityName in qualityNames)
            {
                var quality = (PackQuality)Enum.Parse(typeof(PackQuality), qualityName);
                menu.AddItem(new GUIContent("Import/File/" + qualityName), false, () =>
                {
                    var files = SFB.StandaloneFileBrowser.OpenFilePanel("Select File", "", new[] { new SFB.ExtensionFilter("Image Files", "png", "jpg") }, true);
                    if (files != null && files.Length > 0)
                    {
                        AddFiles(atlas, quality, files);
                    }
                });
                menu.AddItem(new GUIContent("Import/Folder/" + qualityName), false, () =>
                {
                    var files = SFB.StandaloneFileBrowser.OpenFolderPanel("Select Folder", "", true);
                    if (files != null && files.Length > 0)
                    {
                        AddFiles(atlas, quality, files);
                    }
                });
            }
            if (mSelectedSprites.Count > 0)
            {
                foreach (var qualityName in qualityNames)
                {
                    var quality = (PackQuality)Enum.Parse(typeof(PackQuality), qualityName);
                    menu.AddItem(new GUIContent("Quality/" + qualityName), false, () =>
                    {
                        CompressSelected(atlas, quality, mSelectedSprites);
                    });
                }
                menu.AddItem(new GUIContent("Export/Selected"), false, () =>
                {
                    ExportSelected(atlas, mSelectedSprites);
                });
                menu.AddItem(new GUIContent("Export/All"), false, () =>
                {
                    ExportAll(atlas);
                });
                menu.AddSeparator("");
                menu.AddItem(new GUIContent("Delete"), false, () =>
                {
                    DeleteSelected(atlas, mSelectedSprites);
                });
            }
            else
            {
                menu.AddItem(new GUIContent("Export/All"), false, () =>
                {
                    ExportAll(atlas);
                });
            }
            menu.ShowAsContext();
        }
Example #8
0
 public bool Fetch(int atlasIndex, out AtlasRaw atlas)
 {
     atlas = null;
     if (atlasIndex < mAtlases.Count)
     {
         atlas = mAtlases[atlasIndex];
         return(true);
     }
     return(false);
 }
Example #9
0
        private void ExportSelected(AtlasRaw atlas, List <SpriteIndex> selected)
        {
            var folder = EditorUtility.OpenFolderPanel("Select target folder", "", "");

            if (!string.IsNullOrEmpty(folder))
            {
                var exports = selected.Select(sp => atlas.bins[sp.bin].sprites[sp.sprite]).ToArray();
                AtlasPacker.Export(atlas, exports, folder);
            }
        }
Example #10
0
        public static IPackSprite[] ListSprites(AtlasRaw atlasRaw)
        {
            var list = new List <PackAtlasSprite>();

            foreach (var binRaw in atlasRaw.bins)
            {
                foreach (var spriteRaw in binRaw.sprites)
                {
                    list.Add(ParseSprite(binRaw, spriteRaw));
                }
            }
            return(list.ToArray());
        }
Example #11
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());
        }
Example #12
0
        private void ExportAll(AtlasRaw atlas)
        {
            var folder = EditorUtility.OpenFolderPanel("Select target folder", "", "");

            if (!string.IsNullOrEmpty(folder))
            {
                var exports = new List <SpriteRaw>();
                foreach (var bin in atlas.bins)
                {
                    foreach (var sprite in bin.sprites)
                    {
                        exports.Add(sprite);
                    }
                }
                AtlasPacker.Export(atlas, exports.ToArray(), folder);
            }
        }
Example #13
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);
        }
Example #14
0
            public bool Fetch(AtlasRaw atlas, out int atlasIndex)
            {
                atlasIndex = -1;
                var path = AssetDatabase.GetAssetPath(atlas);

                if (!string.IsNullOrEmpty(path))
                {
                    for (int i = 0; i < mAtlases.Count; i++)
                    {
                        if (AssetDatabase.GetAssetPath(mAtlases[i]) == path)
                        {
                            atlasIndex = i;
                            return(true);
                        }
                    }
                }
                return(false);
            }
Example #15
0
        public static SpriteIndex[] SearchSprites(AtlasRaw atlas, int atlasIndex, string matchWord)
        {
            matchWord = matchWord.ToLower();
            var list = new List <SpriteIndex>();

            foreach (var bin in atlas.bins)
            {
                var sprites = bin.sprites;
                for (int i = 0; i < sprites.Length; i++)
                {
                    if (sprites[i].name.ToLower().Contains(matchWord))
                    {
                        list.Add(new SpriteIndex(atlasIndex, sprites[i].bin, i));
                    }
                }
            }
            return(list.ToArray());
        }
Example #16
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());
        }
Example #17
0
 public bool Fetch(SpriteIndex index, out AtlasRaw atlas, out BinRaw bin, out SpriteRaw sprite)
 {
     atlas  = null;
     bin    = null;
     sprite = null;
     if (index.atlas < mAtlases.Count)
     {
         atlas = mAtlases[index.atlas];
         if (index.bin < atlas.bins.Length)
         {
             bin = atlas.bins[index.bin];
             if (index.sprite < bin.sprites.Length)
             {
                 sprite = bin.sprites[index.sprite];
                 return(true);
             }
         }
     }
     return(false);
 }
Example #18
0
 public static void IndexSprite(AtlasRaw atlas, SpriteRaw sprite, out int iBin, out int iSprite)
 {
     iBin    = -1;
     iSprite = -1;
     for (int i = 0; i < atlas.bins.Length; i++)
     {
         var bin = atlas.bins[i];
         for (int j = 0; j < bin.sprites.Length; j++)
         {
             if (bin.sprites[j] == sprite)
             {
                 iSprite = j;
                 break;
             }
         }
         if (iSprite != -1)
         {
             iBin = i;
             break;
         }
     }
 }
Example #19
0
            public bool Fetch(AtlasRaw atlas, string spriteName, out SpriteIndex index)
            {
                index = null;
                var atlasIndex = -1;

                if (Fetch(atlas, out atlasIndex))
                {
                    for (int i = 0; i < atlas.bins.Length; i++)
                    {
                        var bin = atlas.bins[i];
                        for (int j = 0; j < bin.sprites.Length; j++)
                        {
                            var sprite = bin.sprites[j];
                            if (sprite.name == spriteName)
                            {
                                index = new SpriteIndex(atlasIndex, i, j);
                                return(true);
                            }
                        }
                    }
                }
                return(false);
            }
Example #20
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());
            }
        }
Example #21
0
        public void OnGUI(Rect rect, AtlasRaw atlas, BinRaw bin, List <SpriteIndex> selected)
        {
            GUI.BeginClip(rect);
            var localRect      = new Rect(0, 0, rect.width, rect.height);
            var texDragOffset  = mOffset;
            var texScale       = mScale;
            var texScaleOffset = new Vector2(
                localRect.width * (texScale - 1) * 0.5f,
                localRect.height * (texScale - 1) * 0.5f);
            var texOffset = texDragOffset - texScaleOffset;
            var texSize   = localRect.size * texScale;

            AtlasEditorUtil.DrawGrid(localRect);
            AtlasEditorUtil.DrawSprite(localRect, bin.main, bin.addition,
                                       new Rect(-texOffset.x / texSize.x, (texDragOffset.y + texScaleOffset.y) / texSize.y, localRect.width / texSize.x, localRect.height / texSize.y));
            int controlID = GUIUtility.GetControlID(FocusType.Passive);
            var eventType = Event.current.GetTypeForControl(controlID);

            switch (eventType)
            {
            case EventType.ScrollWheel:
            {
                if (localRect.Contains(Event.current.mousePosition))
                {
                    var scaleDelta = 15;
                    texScale      -= Event.current.delta.y / scaleDelta;
                    texScale       = Mathf.Max(texScale, 1);
                    texScaleOffset = new Vector2(localRect.width * (texScale - 1) * 0.5f,
                                                 localRect.height * (texScale - 1) * 0.5f);
                    texDragOffset.x = Mathf.Clamp(texDragOffset.x, -texScaleOffset.x, texScaleOffset.x);
                    texDragOffset.y = Mathf.Clamp(texDragOffset.y, -texScaleOffset.y, texScaleOffset.y);
                    Event.current.Use();
                }
                break;
            }

            case EventType.MouseDown:
            {
                var pos = GUIUtility.GetStateObject(typeof(List <int>), controlID) as List <int>;
                pos.Clear();
                pos.Add((int)Event.current.mousePosition.x);
                pos.Add((int)Event.current.mousePosition.y);
                GUIUtility.hotControl = controlID;
                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl == controlID)
                {
                    var pos = GUIUtility.QueryStateObject(typeof(List <int>), controlID) as List <int>;
                    if (pos[0] <= localRect.width && pos[1] <= localRect.height)
                    {
                        texDragOffset.x += Event.current.delta.x;
                        texDragOffset.y += Event.current.delta.y;
                        texDragOffset.x  = Mathf.Clamp(texDragOffset.x, -texScaleOffset.x, texScaleOffset.x);
                        texDragOffset.y  = Mathf.Clamp(texDragOffset.y, -texScaleOffset.y, texScaleOffset.y);
                        Event.current.Use();
                    }
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == controlID)
                {
                    var pos = GUIUtility.QueryStateObject(typeof(List <int>), controlID) as List <int>;
                    if (pos != null &&
                        Mathf.Abs(pos[0] - (int)Event.current.mousePosition.x) <= 1 &&
                        Mathf.Abs(pos[1] - (int)Event.current.mousePosition.y) <= 1)
                    {
                        var rPos = new Vector2(
                            (pos[0] - texOffset.x) / texSize.x,
                            (pos[1] - texOffset.y) / texSize.y
                            );
                        for (var i = 0; i < bin.sprites.Length; i++)
                        {
                            var sprite     = bin.sprites[i];
                            var spriteRect = sprite.rect;
                            var rRect      = new Rect(
                                spriteRect.x / bin.main.width,
                                (bin.main.height - spriteRect.y - spriteRect.height) / bin.main.height,
                                spriteRect.width / bin.main.width,
                                spriteRect.height / bin.main.height
                                );
                            if (rRect.Contains(rPos))
                            {
                                var index = new SpriteIndex(0, sprite.bin, i);
                                if (Event.current.button == 0)
                                {
                                    if (!mMultiSelectEnabled ||
                                        !Event.current.control)
                                    {
                                        selected.Clear();
                                        selected.Add(index);
                                    }
                                    else
                                    {
                                        if (selected.Any(s => s.bin == index.bin && s.sprite == index.sprite))
                                        {
                                            selected.RemoveAll(s => s.bin == index.bin && s.sprite == index.sprite);
                                        }
                                        else
                                        {
                                            selected.Add(index);
                                        }
                                    }
                                    GUI.FocusControl("");
                                    Event.current.Use();
                                }
                                break;
                            }
                        }
                    }
                    GUIUtility.hotControl = 0;
                }
                break;
            }
            }
            foreach (var index in selected)
            {
                if (atlas.bins[index.bin] == bin)
                {
                    var sprite     = bin.sprites[index.sprite];
                    var spriteRect = sprite.rect;
                    var rRect      = new Rect(
                        spriteRect.x / bin.main.width,
                        (bin.main.height - spriteRect.y - spriteRect.height) / bin.main.height,
                        spriteRect.width / bin.main.width,
                        spriteRect.height / bin.main.height
                        );
                    var tRect = new Rect(
                        rRect.x * texSize.x + texOffset.x,
                        rRect.y * texSize.y + texOffset.y,
                        rRect.width * texSize.x,
                        rRect.height * texSize.y
                        );
                    Handles.color = Color.green;
                    Handles.DrawPolyLine(new Vector3[] {
                        new Vector3(tRect.x, tRect.y, 0),
                        new Vector3(tRect.xMax, tRect.y, 0),
                        new Vector3(tRect.xMax, tRect.yMax, 0),
                        new Vector3(tRect.x, tRect.yMax, 0),
                        new Vector3(tRect.x, tRect.y, 0)
                    });
                    Handles.color = Color.white;
                }
            }
            GUI.EndClip();
            mOffset = texDragOffset;
            mScale  = texScale;
        }