/// <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);
}