Exemplo n.º 1
0
        private void PackFolder(string folder)
        {
            var textures = AssetDatabase.FindAssets("t:Texture2D", new string[] { folder })
                           .Select(guid => AssetDatabase.GUIDToAssetPath(guid))
                           .Select(path => new PackAssetSprite(path)).ToArray();

            if (textures.Length == 0)
            {
                return;
            }
            for (int i = 0; i < textures.Length; i++)
            {
                var texPath    = textures[i].assetPath;
                var assetPath  = texPath.Replace(folder + "/", "");
                var assetDir   = Path.GetDirectoryName(assetPath);
                var assetName  = Path.GetFileNameWithoutExtension(assetPath);
                var assetLabel = string.IsNullOrEmpty(assetDir) ? assetName : assetDir + "/" + assetName;
                textures[i].name    = assetLabel;
                textures[i].quality = PackUtil.CheckTextureCompressed(texPath) ? PackQuality.AlphaSplit : PackQuality.Full;
            }
            var atlasRaw = AtlasPacker.Pack(folder, textures, mSetting);

            if (atlasRaw != null)
            {
                Selection.activeObject = atlasRaw;
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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());
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
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());
        }
Exemplo n.º 6
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());
            }
        }
Exemplo n.º 7
0
        private static void MapAtlas2Sprite(string targetFolder, List <UnityEngine.UI.Sprite> sprites)
        {
            var atlasRaw2AtlasFolder  = new Dictionary <string, string>();
            var atlasRaw2AtlasSprites = new Dictionary <string, Dictionary <string, string> >();

            foreach (var sprite in sprites)
            {
                if (sprite != null && sprite.type == UnityEngine.UI.Sprite.Type.Atlas)
                {
                    var path = AssetDatabase.GetAssetPath(sprite.atlasRaw);
                    if (!string.IsNullOrEmpty(path))
                    {
                        atlasRaw2AtlasFolder[path]  = Path.Combine(targetFolder, Path.GetFileName(Path.GetDirectoryName(path)));
                        atlasRaw2AtlasSprites[path] = new Dictionary <string, string>();
                    }
                }
            }
            foreach (var pair in atlasRaw2AtlasFolder)
            {
                var atlasPath     = pair.Key;
                var targetPath    = pair.Value;
                var atlasFolder   = Path.GetDirectoryName(atlasPath);
                var atlasTag      = Path.GetFileName(atlasFolder);
                var atlasRaw      = AssetDatabase.LoadAssetAtPath <AtlasRaw>(atlasPath);
                var spriteRaws    = atlasRaw.bins.SelectMany(i => i.sprites).ToArray();
                var exportSprites = AtlasPacker.Export(atlasRaw, spriteRaws, targetPath);
                for (int i = 0; i < exportSprites.Length; i++)
                {
                    atlasRaw2AtlasSprites[atlasPath][spriteRaws[i].name] = exportSprites[i];
                    var maxTextureSize = PackUtil.Scale2POT((int)Mathf.Max(spriteRaws[i].rect.width, spriteRaws[i].rect.height));
                    var binRaw         = atlasRaw.bins[spriteRaws[i].bin];
                    var compressed     = (PackQuality)binRaw.quality != PackQuality.Full;
                    var tranparency    = PackUtil.CheckAtlasBinTranparency(binRaw);
                    var importer       = (TextureImporter)AssetImporter.GetAtPath(exportSprites[i]);
                    importer.textureType         = TextureImporterType.Sprite;
                    importer.spritePivot         = spriteRaws[i].pivot;
                    importer.spriteBorder        = spriteRaws[i].border;
                    importer.spritePackingTag    = atlasTag;
                    importer.isReadable          = false;
                    importer.maxTextureSize      = maxTextureSize;
                    importer.mipmapEnabled       = false;
                    importer.wrapMode            = TextureWrapMode.Clamp;
                    importer.npotScale           = TextureImporterNPOTScale.None;
                    importer.textureCompression  = TextureImporterCompression.Uncompressed;
                    importer.alphaIsTransparency = true;
                    if (compressed)
                    {
                        importer.SetPlatformTextureSettings(new TextureImporterPlatformSettings()
                        {
                            name               = "Standalone",
                            overridden         = true,
                            maxTextureSize     = maxTextureSize,
                            compressionQuality = (int)TextureCompressionQuality.Normal,
                            format             = TextureImporterFormat.DXT5,
                        });
                        importer.SetPlatformTextureSettings(new TextureImporterPlatformSettings()
                        {
                            name               = "iPhone",
                            overridden         = true,
                            maxTextureSize     = maxTextureSize,
                            compressionQuality = (int)TextureCompressionQuality.Normal,
                            format             = tranparency ? TextureImporterFormat.RGBA16 : TextureImporterFormat.RGB16,
                        });
                        importer.SetPlatformTextureSettings(new TextureImporterPlatformSettings()
                        {
                            name               = "Android",
                            overridden         = true,
                            maxTextureSize     = maxTextureSize,
                            compressionQuality = (int)TextureCompressionQuality.Normal,
                            format             = tranparency ? TextureImporterFormat.ETC2_RGBA8 : TextureImporterFormat.ETC2_RGB4,
                        });
                    }
                    else
                    {
                        importer.SetPlatformTextureSettings(new TextureImporterPlatformSettings()
                        {
                            name       = "Standalone",
                            overridden = false,
                        });
                        importer.SetPlatformTextureSettings(new TextureImporterPlatformSettings()
                        {
                            name       = "iPhone",
                            overridden = false,
                        });
                        importer.SetPlatformTextureSettings(new TextureImporterPlatformSettings()
                        {
                            name       = "Android",
                            overridden = false,
                        });
                    }
                    importer.SaveAndReimport();
                }
            }
            for (int i = 0; i < sprites.Count; i++)
            {
                var sprite = sprites[i];
                if (sprite != null && sprite.type == UnityEngine.UI.Sprite.Type.Atlas)
                {
                    var atlasPath = AssetDatabase.GetAssetPath(sprite.atlasRaw);
                    if (atlasRaw2AtlasSprites.ContainsKey(atlasPath))
                    {
                        var atlasSprites = atlasRaw2AtlasSprites[atlasPath];
                        if (atlasSprites.ContainsKey(sprites[i].spriteName))
                        {
                            var spritePath  = atlasSprites[sprites[i].spriteName];
                            var unitySprite = AssetDatabase.LoadAssetAtPath <UnityEngine.Sprite>(spritePath);
                            sprites[i] = new UnityEngine.UI.Sprite(unitySprite);
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        private static void MapSprite2Atlas(string targetFolder, PackSetting setting, List <UnityEngine.UI.Sprite> sprites)
        {
            var spritePaths = new List <string>();

            foreach (var sprite in sprites)
            {
                if (sprite != null && sprite.type == UnityEngine.UI.Sprite.Type.Sprite)
                {
                    var path = AssetDatabase.GetAssetPath(sprite.sprite);
                    if (!string.IsNullOrEmpty(path) && !spritePaths.Contains(path))
                    {
                        spritePaths.Add(path);
                    }
                }
            }
            var pathSpriteMap = new Dictionary <string, UnityEngine.UI.Sprite>();
            var spriteGroups  = spritePaths.GroupBy(path =>
            {
                var importer = AssetImporter.GetAtPath(path) as TextureImporter;
                return(importer.spritePackingTag);
            });

            foreach (var group in spriteGroups)
            {
                var groupPaths   = group.ToArray();
                var groupNames   = Util.MapSpritePath2SpriteNameInAtlas(groupPaths);
                var groupSprites = new PackAssetSprite[groupPaths.Length];
                for (int i = 0; i < groupPaths.Length; i++)
                {
                    groupSprites[i] = new PackAssetSprite(groupPaths[i])
                    {
                        name = groupNames[i]
                    };
                    groupSprites[i].quality = PackUtil.CheckTextureCompressed(groupPaths[i]) ? PackQuality.AlphaSplit : PackQuality.Full;
                }
                var groupTag    = string.IsNullOrEmpty(group.Key) ? EmptyTagName : group.Key;
                var groupFolder = Path.Combine(targetFolder, groupTag);
                if (!Directory.Exists(groupFolder))
                {
                    Directory.CreateDirectory(groupFolder);
                }
                var atlasRaw = AtlasPacker.Pack(groupFolder, groupSprites, setting);
                for (int i = 0; i < groupPaths.Length; i++)
                {
                    pathSpriteMap[groupPaths[i]] = new UnityEngine.UI.Sprite(atlasRaw, groupNames[i]);
                }
            }
            ;
            for (int i = 0; i < sprites.Count; i++)
            {
                var sprite = sprites[i];
                if (sprite != null && sprite.type == UnityEngine.UI.Sprite.Type.Sprite)
                {
                    var path = AssetDatabase.GetAssetPath(sprite.sprite);
                    if (!string.IsNullOrEmpty(path))
                    {
                        sprites[i] = pathSpriteMap[path];
                    }
                }
            }
        }
Exemplo n.º 9
0
        public override void OnInspectorGUI()
        {
            OnValidateSelected();
            var atlas = target as AtlasRaw;

            EditorGUILayout.GetControlRect(false, 2);
            int clickIndex = AtlasEditorUtil.TitleBar(new GUIContent[]
            {
                new GUIContent("+File"),
                new GUIContent("+Folder"),
            });

            if (clickIndex == 0)
            {
                DisplayImportMenu(atlas, false);
            }
            else if (clickIndex == 1)
            {
                DisplayImportMenu(atlas, true);
            }
            mRepackFold = AtlasEditorUtil.ToggleBar(new GUIContent("Settings"), mRepackFold);
            if (mRepackFold)
            {
                if (!mPackDataInit)
                {
                    mPackDataInit = true;
                    mSetting      = new PackSetting(atlas.maxSize, atlas.padding, atlas.isPOT, atlas.forceSquare);
                }
                EditorGUI.indentLevel += 1;
                mSetting.maxAtlasSize  = EditorGUILayout.IntPopup("Max Size", mSetting.maxAtlasSize, Array.ConvertAll(PackConst.AtlasSizeList, value => value.ToString()), PackConst.AtlasSizeList);
                mSetting.padding       = EditorGUILayout.IntField("Padding", mSetting.padding);
                mSetting.isPOT         = EditorGUILayout.Toggle("Power Of 2", mSetting.isPOT);
                GUI.enabled            = mSetting.isPOT;
                if (!mSetting.isPOT)
                {
                    mSetting.forceSquare = false;
                }
                mSetting.forceSquare = EditorGUILayout.Toggle("Force Square", mSetting.forceSquare);
                GUI.enabled          = true;
                var rect = EditorGUILayout.GetControlRect(false, 20);
                if (GUI.Button(new Rect(rect.center.x - 75, rect.y, 150, rect.height), "Repack"))
                {
                    AtlasPacker.Repack(atlas, null, mSetting);
                }
                EditorGUI.indentLevel -= 1;
                EditorGUILayout.Space();
            }
            EditorGUI.BeginChangeCheck();
            mSearchPattern = AtlasEditorUtil.SearchBar(new GUIContent("Search Sprite"), mSearchPattern);
            if (EditorGUI.EndChangeCheck() || mAtlasDirty)
            {
                mSearchResults.Clear();
                if (!string.IsNullOrEmpty(mSearchPattern))
                {
                    mSearchResults.AddRange(AtlasEditorUtil.SearchSprites(atlas, 0, mSearchPattern));
                }
            }
            if (mAtlasDirty)
            {
                mAtlasDirty  = false;
                mSelectedBin = Mathf.Clamp(mSelectedBin, 0, atlas.bins.Length - 1);
                mSelectedSprites.Clear();
            }
            EditorGUI.indentLevel += 1;
            if (!string.IsNullOrEmpty(mSearchPattern))
            {
                EditorGUILayout.LabelField(string.Format("{0} results", mSearchResults.Count));
            }
            if (mSearchResults != null && mSearchResults.Count > 0)
            {
                OnValidateResult();
                OnSearchResultGUI();
            }
            EditorGUI.indentLevel -= 1;
            if (atlas.bins.Length > 0)
            {
                var titleRect = EditorGUILayout.GetControlRect(false, EditorStyles.toolbar.fixedHeight);
                var controlId = GUIUtility.GetControlID(FocusType.Passive);
                var eventType = Event.current.GetTypeForControl(controlId);
                if (eventType == EventType.Repaint)
                {
                    EditorStyles.toolbar.Draw(titleRect, GUIContent.none, controlId);
                }
                var binNames   = Array.ConvertAll(atlas.bins, i => PackUtil.GetDisplayName(i));
                var binIndexes = new int[binNames.Length];
                for (int i = 0; i < binNames.Length; i++)
                {
                    binIndexes[i] = i;
                }
                mSelectedBin = EditorGUI.IntPopup(new Rect(10, titleRect.y, titleRect.width - 10, titleRect.height), "Preview Bin", mSelectedBin, binNames, binIndexes, EditorStyles.toolbarPopup);
                mSelectedBin = Mathf.Clamp(mSelectedBin, 0, atlas.bins.Length - 1);
                EditorGUILayout.Space();
                var bin         = atlas.bins[mSelectedBin];
                var previewRect = EditorGUILayout.GetControlRect(false, 512);
                previewRect.width  = Mathf.Min(previewRect.width, (float)bin.main.width / bin.main.height * previewRect.height);
                previewRect.height = (float)bin.main.height / bin.main.width * previewRect.width;
                OnPreviewBin(previewRect);
            }
        }