public static string[] ExportAtlasTextures(MA_TextureAtlasserProAtlas atlas, TextureExportSettings textureExportSettings, string savePath = EXPORT_ASSET_PATH, string tempPath = TEXTURE_ATLASSER_PATH) { switch (textureExportSettings.textureFormat) { case TextureFormat.None: break; case TextureFormat.Png: return(ExportAtlasPNG(atlas, textureExportSettings, savePath)); default: break; } return(null); }
private static void SetAtlasSpriteSettings(MA_TextureAtlasserProAtlas atlas, TextureExportSettings textureExportSettings, string savePath = EXPORT_ASSET_PATH) { //Foreach texture group for (int i = 0; i < atlas.textureGroupRegistration.Count; i++) { //Convert string textureName = atlas.name + "_" + atlas.textureGroupRegistration[i].name + '.' + textureExportSettings.textureFormat.ToString(); TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(savePath + textureName); textureImporter.textureType = TextureImporterType.Sprite; textureImporter.alphaIsTransparency = true; //Slice sprites. if (textureExportSettings.textureType == TextureType.SpriteSliced) { textureImporter.spriteImportMode = SpriteImportMode.None; //Reset it to update? textureImporter.spriteImportMode = SpriteImportMode.Multiple; List <SpriteMetaData> spriteMetaData = new List <SpriteMetaData>(); foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { if (q.textureGroups != null && q.textureGroups[i].texture != null) { //Create new SpriteMetaData. SpriteMetaData smd = new SpriteMetaData { name = q.name, rect = new Rect(q.guiRect.x, atlas.textureAtlasSize.y - q.guiRect.y - q.guiRect.height, q.guiRect.width, q.guiRect.height) }; spriteMetaData.Add(smd); } } textureImporter.spritesheet = spriteMetaData.ToArray(); } else { textureImporter.spriteImportMode = SpriteImportMode.Single; } textureImporter.SaveAndReimport(); } }
private static string[] ExportAtlasPNG(MA_TextureAtlasserProAtlas atlas, TextureExportSettings textureExportSettings, string savePath = EXPORT_ASSET_PATH, string tempPath = TEMP_ASSET_PATH) { if (atlas == null || atlas.textureQuads == null || atlas.textureGroupRegistration == null) { return(null); } string[] assetPaths = new string[atlas.textureGroupRegistration.Count]; //Create temp folder CreateFolder(tempPath); //Foreach texture group for (int i = 0; i < atlas.textureGroupRegistration.Count; i++) { //Create new Texture Atlas Texture2D newTexture = new Texture2D((int)atlas.textureAtlasSize.x, (int)atlas.textureAtlasSize.y) { name = atlas.name + "_" + atlas.textureGroupRegistration[i].name }; foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { if (q.textureGroups != null && q.textureGroups[i].texture != null) { //Make temp copy string orginalTexturePath = AssetDatabase.GetAssetPath(q.textureGroups[i].texture); string orginalTextureExtension = System.IO.Path.GetExtension(orginalTexturePath); string tempTexturePath = tempPath + q.textureGroups[i].texture.name + orginalTextureExtension; AssetDatabase.CopyAsset(orginalTexturePath, tempTexturePath); //Set temp copy to default settings TextureImporter tempTextureImporter = (TextureImporter)AssetImporter.GetAtPath(tempTexturePath); tempTextureImporter.textureType = TextureImporterType.Default; tempTextureImporter.sRGBTexture = false; tempTextureImporter.alphaIsTransparency = false; tempTextureImporter.maxTextureSize = (int)Mathf.Max(atlas.textureAtlasSize.x, atlas.textureAtlasSize.y); tempTextureImporter.textureCompression = TextureImporterCompression.Uncompressed; tempTextureImporter.SaveAndReimport(); //Load temp copy Texture tempCopy = AssetDatabase.LoadAssetAtPath <Texture>(tempTextureImporter.assetPath); //Create new texture part Texture2D newTexturePart = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(tempCopy); //Scale it newTexturePart = newTexturePart.MA_Scale2D((int)q.guiRect.width, (int)q.guiRect.height, textureExportSettings.textureScaleMode); //Add it newTexture = newTexture.MA_Combine2D(newTexturePart, (int)q.guiRect.x, (int)q.guiRect.y); //Delete temp copy AssetDatabase.DeleteAsset(tempTextureImporter.assetPath); } } //Save it newTexture.MA_Save2D(newTexture.name, savePath); assetPaths[i] = (savePath + newTexture.name + '.' + textureExportSettings.textureFormat.ToString()); //Set settings. switch (textureExportSettings.textureType) { case TextureType.Default: { TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(savePath + newTexture.name + '.' + textureExportSettings.textureFormat.ToString()); textureImporter.textureType = TextureImporterType.Default; textureImporter.SaveAndReimport(); } break; case TextureType.Sprite: SetAtlasSpriteSettings(atlas, textureExportSettings, savePath); break; case TextureType.SpriteSliced: SetAtlasSpriteSettings(atlas, textureExportSettings, savePath); break; default: break; } } //Delete temp folder DeleteFolder(tempPath); //Refresh AssetDatabase.Refresh(); return(assetPaths); }