/// <summary> /// Filters the resources for atlas import. /// </summary> /// <returns>The resources for atlas import.</returns> /// <param name="resources">Resources.</param> public static Object[] FilterResourcesForAtlasImport(Object[] resources) { List <Object> tempList = new List <Object>(); foreach (Object resource in resources) { string resourcePath = SPTools.GetAssetPath(resource); // Check if this is a main asset and queue all it's sub assets if (SPTools.IsMainAsset(resource) && SPTools.HasSubAssets(resource)) { Object[] subAssets = SPTools.FilterResourcesForAtlasImport(SPTools.GetSubAssets(resource)); foreach (Object a in subAssets) { tempList.Add(a); } } else if (resource is Texture2D || resource is Sprite) { tempList.Add(resource); } else if (SPTools.IsDirectory(resourcePath)) { Object[] subAssets = SPTools.FilterResourcesForAtlasImport(SPTools.GetDirectoryAssets(resourcePath)); foreach (Object a in subAssets) { tempList.Add(a); } } } return(tempList.ToArray()); }
/// <summary> /// Corrects the textures format. /// </summary> /// <param name="spriteInfoList">Sprite info list.</param> protected void CorrectTexturesFormat(List <SPSpriteInfo> spriteInfoList) { if (spriteInfoList == null || spriteInfoList.Count == 0) { return; } foreach (SPSpriteInfo spriteInfo in spriteInfoList) { Texture2D texture = null; // No source but present target sprite if (spriteInfo.source == null && spriteInfo.targetSprite != null) { texture = spriteInfo.targetSprite.texture; } // Texture source else if (spriteInfo.source is Texture2D) { texture = (spriteInfo.source as Texture2D); } // Sprite source else if (spriteInfo.source is Sprite) { texture = (spriteInfo.source as Sprite).texture; } if (texture != null) { // Make sure it's the correct format if (texture.format != TextureFormat.ARGB32 && texture.format != TextureFormat.RGBA32 && texture.format != TextureFormat.BGRA32 && texture.format != TextureFormat.RGB24 && texture.format != TextureFormat.Alpha8 && texture.format != TextureFormat.DXT1 && texture.format != TextureFormat.DXT5) { // Get the texture asset path string assetPath = SPTools.GetAssetPath(texture); // Set new texture format if (!SPTools.AssetSetFormat(assetPath, TextureImporterFormat.ARGB32)) { Debug.LogWarning("Sprite Packer failed to set texture format ARGB32 on asset: " + assetPath); } } } } }
/// <summary> /// Loads a sprite from a texture. /// </summary> /// <returns>The sprite.</returns> /// <param name="mainTexture">Main texture.</param> /// <param name="name">Name.</param> public static Sprite LoadSprite(Texture2D mainTexture, string name) { string texturePath = SPTools.GetAssetPath(mainTexture); Object[] atlasAssets = AssetDatabase.LoadAllAssetsAtPath(texturePath); foreach (Object asset in atlasAssets) { if (AssetDatabase.IsSubAsset(asset) && asset.name == name) { return(asset as Sprite); } } return(null); }
/// <summary> /// Collects the source textures asset paths. /// </summary> /// <returns>The source texture asset paths.</returns> /// <param name="spriteInfoList">Sprite info list.</param> protected string[] CollectSourceTextureAssetPaths(List <SPSpriteInfo> spriteInfoList) { List <string> texturePaths = new List <string>(); // Add the textures from the sprite info list into our textures list foreach (SPSpriteInfo spriteInfo in spriteInfoList) { string path = string.Empty; // No source but present target sprite if (spriteInfo.source == null && spriteInfo.targetSprite != null) { path = SPTools.GetAssetPath(spriteInfo.targetSprite.texture); } // Texture source else if (spriteInfo.source is Texture2D) { path = SPTools.GetAssetPath(spriteInfo.source as Texture2D); } // Sprite source else if (spriteInfo.source is Sprite) { path = SPTools.GetAssetPath((spriteInfo.source as Sprite).texture); } if (!string.IsNullOrEmpty(path)) { if (!texturePaths.Contains(path)) { texturePaths.Add(path); } } } return(texturePaths.ToArray()); }
/// <summary> /// Imports and configures atlas texture. /// </summary> /// <returns><c>true</c>, if import and configure atlas texture was successful, <c>false</c> otherwise.</returns> /// <param name="targetTexture">Target texture.</param> /// <param name="sourceTexture">Source texture.</param> /// <param name="uvs">Uvs.</param> /// <param name="names">Names.</param> /// <param name="defaultPivot">Default pivot.</param> /// <param name="defaultCustomPivot">Default custom pivot.</param> public static bool ImportAndConfigureAtlasTexture(Texture2D targetTexture, Texture2D sourceTexture, Rect[] uvs, SPSpriteImportData[] spritesImportData) { // Get the asset path string assetPath = SPTools.GetAssetPath(targetTexture); if (string.IsNullOrEmpty(assetPath)) { Debug.LogError("Sprite Packer failed to Import and Configure the atlas texture, reason: Could not resolve asset path."); return(false); } // Clear the read-only flag in texture file attributes if (!SPTools.RemoveReadOnlyFlag(assetPath)) { Debug.LogError("Sprite Packer failed to Import and Configure the atlas texture, reason: Could not remove the readonly flag from the asset."); return(false); } // Write the source texture data to the asset byte[] bytes = sourceTexture.EncodeToPNG(); System.IO.File.WriteAllBytes(assetPath, bytes); bytes = null; // Get the asset texture importer TextureImporter texImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; if (texImporter == null) { Debug.LogError("Sprite Packer failed to Import and Configure the atlas texture, reason: Could not get the texture importer for the asset."); return(false); } // Get the asset texture importer settings TextureImporterSettings texImporterSettings = new TextureImporterSettings(); // Apply sprite type texImporter.textureType = TextureImporterType.Sprite; texImporter.spriteImportMode = SpriteImportMode.Multiple; // Configure the spritesheet meta data SpriteMetaData[] spritesheetMeta = new SpriteMetaData[uvs.Length]; for (int i = 0; i < uvs.Length; i++) { if (SPTools.HasSpritesheetMeta(texImporter.spritesheet, spritesImportData[i].name)) { SpriteMetaData currentMeta = SPTools.GetSpritesheetMeta(texImporter.spritesheet, spritesImportData[i].name); Rect currentRect = uvs[i]; currentRect.x *= sourceTexture.width; currentRect.width *= sourceTexture.width; currentRect.y *= sourceTexture.height; currentRect.height *= sourceTexture.height; currentMeta.rect = currentRect; spritesheetMeta[i] = currentMeta; } else { SpriteMetaData currentMeta = new SpriteMetaData(); Rect currentRect = uvs[i]; currentRect.x *= sourceTexture.width; currentRect.width *= sourceTexture.width; currentRect.y *= sourceTexture.height; currentRect.height *= sourceTexture.height; currentMeta.rect = currentRect; currentMeta.name = spritesImportData[i].name; currentMeta.alignment = (int)spritesImportData[i].alignment; currentMeta.pivot = spritesImportData[i].pivot; currentMeta.border = spritesImportData[i].border; spritesheetMeta[i] = currentMeta; } } texImporter.spritesheet = spritesheetMeta; // Read the texture importer settings texImporter.ReadTextureSettings(texImporterSettings); // Disable Read/Write texImporterSettings.readable = false; // Re-set the texture importer settings texImporter.SetTextureSettings(texImporterSettings); // Save and Reimport the asset AssetDatabase.SaveAssets(); SPTools.DoAssetReimport(assetPath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport); // Return success return(true); }
/// <summary> /// Sets the texture asset Read/Write enabled state. /// </summary> /// <returns><c>true</c>, if set read write enabled was textured, <c>false</c> otherwise.</returns> /// <param name="texture">Texture.</param> /// <param name="enabled">If set to <c>true</c> enabled.</param> /// <param name="force">If set to <c>true</c> force.</param> public static bool TextureSetReadWriteEnabled(Texture2D texture, bool enabled, bool force) { return(SPTools.AssetSetReadWriteEnabled(SPTools.GetAssetPath(texture), enabled, force)); }