Пример #1
0
 public PackAtlasSprite(BinRaw binRaw, SpriteRaw spriteRaw)
 {
     mMain = binRaw.main;
     Debug.Assert(mMain != null, "Can not find main texture.");
     mAddition     = binRaw.addition;
     mName         = spriteRaw.name;
     mRect         = spriteRaw.rect;
     mBorder       = spriteRaw.border;
     mPivot        = spriteRaw.pivot;
     mTransparency = PackUtil.CheckAtlasBinTranparency(binRaw);
     mQuality      = (PackQuality)binRaw.quality;
 }
Пример #2
0
 public void Setup(PackSetting setting, BinRaw bin)
 {
     this.setting  = setting;
     this.bin      = bin;
     mainAsset     = bin.main;
     mainAssetPath = AssetDatabase.GetAssetPath(mainAsset);
     mainImporter  = AssetImporter.GetAtPath(mainAssetPath) as TextureImporter;
     if (bin.addition != null)
     {
         addAsset     = bin.addition;
         addAssetPath = AssetDatabase.GetAssetPath(addAsset);
         addImporter  = AssetImporter.GetAtPath(addAssetPath) as TextureImporter;
     }
     transparency   = PackUtil.CheckAtlasBinTranparency(bin);
     maxTextureSize = PackUtil.Scale2POT(Mathf.Max((int)bin.size.x, (int)bin.size.y));
 }
Пример #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);
                        }
                    }
                }
            }
        }