static bool UpdateTexture (UIAtlas atlas, List<SpriteEntry> sprites)
{
// Get the texture for the atlas
Texture2D tex = atlas.texture as Texture2D;
string oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : "";
string newPath = NGUIEditorTools.GetSaveableTexturePath(atlas);
// Clear the read-only flag in texture file attributes
if (System.IO.File.Exists(newPath))
{
System.IO.FileAttributes newPathAttrs = System.IO.File.GetAttributes(newPath);
newPathAttrs &= ~System.IO.FileAttributes.ReadOnly;
System.IO.File.SetAttributes(newPath, newPathAttrs);
}
NGUIEditorTools.ImportTexture(oldPath, true, false);
tex
= new Texture2D
(1,
1, TextureFormat
.ARGB32,
false);
// Pack the sprites into this texture
if (PackTextures(tex, sprites))
{
byte[] bytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes(newPath, bytes);
bytes = null;
// Load the texture we just saved as a Texture2D
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
tex = NGUIEditorTools.ImportTexture(newPath, false, true);
// Update the atlas texture
if (tex == null) Debug.LogError("Failed to load the created atlas saved as " + newPath);
else atlas.spriteMaterial.mainTexture = tex;
ReleaseSprites(sprites);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return true;
}
else
{
//Debug.LogError("Operation canceled: The selected sprites can't fit into the atlas.\n" +
// "Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead.");
EditorUtility.DisplayDialog("Operation Canceled", "The selected sprites can't fit into the atlas.\n" +
"Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead", "OK");
return false;
}
}