public static CreateBlankTexture ( string path, bool alphaTransparency ) : bool | ||
path | string | Asset Path. |
alphaTransparency | bool | If set to |
return | bool |
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();
textureImporter.ReadTextureSettings(settings);
settings.spriteMode = 2;
settings.readable = false;
settings.maxTextureSize = 4096;
settings.wrapMode = TextureWrapMode.Clamp;
settings.npotScale = TextureImporterNPOTScale.ToNearest;
settings.textureFormat = 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;
}
public static void CreateInstance() { string assetPath = GetSavePath(); if (string.IsNullOrEmpty(assetPath)) { return; } // Create the sprite packer instance SPInstance asset = ScriptableObject.CreateInstance("SPInstance") as SPInstance; AssetDatabase.CreateAsset(asset, AssetDatabase.GenerateUniqueAssetPath(assetPath)); AssetDatabase.Refresh(); // Save the instance id in the editor prefs EditorPrefs.SetInt(SPTools.Settings_SavedInstanceIDKey, asset.GetInstanceID()); // Repaint the SPDropWindow EditorWindow.GetWindow(typeof(SPDropWindow)).Repaint(); // Get a name for the texture string texturePath = assetPath.Replace(".asset", ".png"); // Create blank texture if (SPTools.CreateBlankTexture(texturePath, true)) { // Set the texture reff in the sprite packer instance asset.texture = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D; } // Focus on the new sprite packer EditorUtility.FocusProjectWindow(); Selection.activeObject = asset; }