コード例 #1
0
ファイル: AtlasRawEditor.cs プロジェクト: SylarLi/AtlaS
        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);
            }
        }
コード例 #2
0
ファイル: AtlasRawEditor.cs プロジェクト: SylarLi/AtlaS
        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);
            }
        }
コード例 #3
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);
                        }
                    }
                }
            }
        }