using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
public class ExtractSprite : EditorWindow {
//UIAtlas selectedAtlas;
string path;
// Add menu named "My Window" to the Window menu
[MenuItem ("NGUI/Extract Sprites")]
static void Init () {
// Get existing open window or if none, make a new one:
ExtractSprite window
= (ExtractSprite
)EditorWindow
.GetWindow (typeof (ExtractSprite
)); }
void OnSelectAtlas (MonoBehaviour obj)
{
path = AssetDatabase.GetAssetPath (obj);
path = path.Substring (0, path.LastIndexOf ("/"));
path = path.Substring (6);
Debug.Log(path);
NGUISettings.atlas = obj as UIAtlas;
Repaint();
}
void OnGUI () {
bool extract = false;
GUILayout.Label ("Select atlas", EditorStyles.boldLabel);
ComponentSelector.Draw<UIAtlas>("Select", NGUISettings.atlas, OnSelectAtlas);
extract = GUILayout.Button("Extract", GUILayout.Width(76f));
if (extract) {
List
<SpriteEntry
> sprites
= new List
<SpriteEntry
>(); ExtractSprites(NGUISettings.atlas, sprites, path);
}
}
class SpriteEntry : UISpriteData
{
// Sprite texture -- original texture or a temporary texture
public Texture2D tex;
// Whether the texture is temporary and should be deleted
public bool temporaryTexture = false;
}
static void ExtractSprites (UIAtlas atlas, List<SpriteEntry> finalSprites, string _path)
{
// Make the atlas texture readable
Texture2D atlasTex = NGUIEditorTools.ImportTexture(atlas.texture, true, false, !atlas.premultipliedAlpha);
if (atlasTex != null)
{
Color32[] oldPixels = null;
int oldWidth = atlasTex.width;
int oldHeight = atlasTex.height;
List<UISpriteData> existingSprites = atlas.spriteList;
foreach (UISpriteData es in existingSprites)
{
bool found = false;
foreach (SpriteEntry fs in finalSprites)
{
if (es.name == fs.name)
{
fs.CopyBorderFrom(es);
found = true;
break;
}
}
if (!found)
{
// Read the atlas
if (oldPixels == null) oldPixels = atlasTex.GetPixels32();
int xmin = Mathf.Clamp(es.x, 0, oldWidth);
int ymin = Mathf.Clamp(es.y, 0, oldHeight);
int newWidth = Mathf.Clamp(es.width, 0, oldWidth);
int newHeight = Mathf.Clamp(es.height, 0, oldHeight);
if (newWidth == 0 || newHeight == 0) continue;
Color32
[] newPixels
= new Color32
[newWidth
* newHeight
];
for (int y = 0; y < newHeight; ++y)
{
for (int x = 0; x < newWidth; ++x)
{
int newIndex = (newHeight - 1 - y) * newWidth + x;
int oldIndex = (oldHeight - 1 - (ymin + y)) * oldWidth + (xmin + x);
newPixels[newIndex] = oldPixels[oldIndex];
}
}
// Create a new sprite
SpriteEntry sprite
= new SpriteEntry
(); sprite.CopyFrom(es);
sprite.SetRect(0, 0, newWidth, newHeight);
//sprite.temporaryTexture = true;
sprite.temporaryTexture = false;
sprite
.tex = new Texture2D
(newWidth, newHeight
); sprite.tex.SetPixels32(newPixels);
sprite.tex.Apply();
finalSprites.Add(sprite);
// Encode texture into PNG
var bytes = sprite.tex.EncodeToPNG();
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + _path + "/" +es.name+".png", bytes);
}
}
}
// The atlas no longer needs to be readable
NGUIEditorTools.ImportTexture(atlas.texture, false, false, !atlas.premultipliedAlpha);
}
}