/// <summary> /// Creates a blank atlas texture. /// </summary> /// <returns><c>true</c>, if blank texture was created, <c>false</c> otherwise.</returns> /// <param name="path">Asset Path.</param> /// <param name="alphaTransparency">If set to <c>true</c> alpha transparency.</param> public static bool CreateBlankTexture(string path, bool alphaTransparency) { if (string.IsNullOrEmpty(path)) { return(false); } // Prepare blank texture Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, false); // Create the texture asset AssetDatabase.CreateAsset(texture, AssetDatabase.GenerateUniqueAssetPath(path)); // Clear the read-only flag in texture file attributes if (!SPTools.RemoveReadOnlyFlag(path)) { return(false); } // Write the texture data byte[] bytes = texture.EncodeToPNG(); System.IO.File.WriteAllBytes(path, bytes); bytes = null; // Get the asset texture importer TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; if (textureImporter == null) { return(false); } TextureImporterSettings settings = new TextureImporterSettings(); TextureImporterPlatformSettings platformSetting = new TextureImporterPlatformSettings(); textureImporter.ReadTextureSettings(settings); settings.spriteMode = 2; settings.readable = false; textureImporter.maxTextureSize = 4096; settings.wrapMode = TextureWrapMode.Clamp; settings.npotScale = TextureImporterNPOTScale.ToNearest; platformSetting.format = TextureImporterFormat.ARGB32; settings.filterMode = FilterMode.Point; settings.aniso = 4; settings.alphaIsTransparency = alphaTransparency; textureImporter.SetTextureSettings(settings); textureImporter.textureType = TextureImporterType.Sprite; AssetDatabase.SaveAssets(); SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport); return(true); }
/// <summary> /// Imports a texture as asset. /// </summary> /// <returns><c>true</c>, if texture was imported, <c>false</c> otherwise.</returns> /// <param name="path">Path.</param> /// <param name="texture">Texture.</param> public static bool ImportTexture(string path, Texture2D texture) { if (string.IsNullOrEmpty(path)) { return(false); } // Clear the read-only flag in texture file attributes if (!SPTools.RemoveReadOnlyFlag(path)) { return(false); } byte[] bytes = texture.EncodeToPNG(); System.IO.File.WriteAllBytes(path, bytes); bytes = null; AssetDatabase.SaveAssets(); SPTools.DoAssetReimport(path, ImportAssetOptions.ForceSynchronousImport); return(true); }
/// <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> /// Creates a blank atlas texture. /// </summary> /// <returns><c>true</c>, if blank texture was created, <c>false</c> otherwise.</returns> /// <param name="path">Asset Path.</param> /// <param name="alphaTransparency">If set to <c>true</c> alpha transparency.</param> public static bool CreateBlankTexture(string path, bool alphaTransparency) { if (string.IsNullOrEmpty(path)) { return(false); } // Prepare blank texture Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, false); // Create the texture asset byte[] bytes = texture.EncodeToPNG(); FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter writer = new BinaryWriter(stream); for (int i = 0; i < bytes.Length; i++) { writer.Write(bytes[i]); } writer.Close(); stream.Close(); // Clear the read-only flag in texture file attributes if (!SPTools.RemoveReadOnlyFlag(path)) { return(false); } // Import the texture asset SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate); // Get the asset texture importer TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; if (textureImporter == null) { return(false); } TextureImporterSettings settings = new TextureImporterSettings(); textureImporter.ReadTextureSettings(settings); //TextureImporterPlatformSettings settings.spriteMode = 2; settings.readable = false; //settings.maxTextureSize = 4096; settings.wrapMode = TextureWrapMode.Clamp; settings.npotScale = TextureImporterNPOTScale.ToNearest; //settings.textureFormat = TextureImporterFormat.ARGB32; settings.filterMode = FilterMode.Trilinear; settings.aniso = 4; settings.alphaIsTransparency = alphaTransparency; textureImporter.SetTextureSettings(settings); textureImporter.textureType = TextureImporterType.Sprite; TextureImporterPlatformSettings tips = new TextureImporterPlatformSettings(); tips.maxTextureSize = 4096; tips.format = TextureImporterFormat.Automatic; tips.textureCompression = TextureImporterCompression.Uncompressed; tips.overridden = true; textureImporter.SetPlatformTextureSettings(tips); AssetDatabase.SaveAssets(); SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport); return(true); }