Skip to content

Instantly share code, notes, and snippets.

@arkms
Last active November 2, 2017 18:07
Show Gist options
  • Save arkms/eb25526128a825591132791760725149 to your computer and use it in GitHub Desktop.
Save arkms/eb25526128a825591132791760725149 to your computer and use it in GitHub Desktop.
Guardar todos los sprites de una textura recortados dentro de la herramienta de Unity como imagenes separadas png para usarlo en otra herramienta
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class SpriteToTexture : EditorWindow
{
private Texture2D Atlas;
[MenuItem("Window/SpriteToTexture")]
public static void Init()
{
EditorWindow.GetWindow(typeof(SpriteToTexture), true, "Sprite To Texture");
}
void OnGUI()
{
Atlas = (Texture2D)EditorGUILayout.ObjectField("Atlas", Atlas, typeof(Texture2D), false);
if (Atlas) //Ya se asigno una textura?
{
if (GUILayout.Button("Convert"))
{
ConvertAtlas2Prefabs();
}
}
}
void ConvertAtlas2Prefabs()
{
string path= AssetDatabase.GetAssetPath(Atlas);
//Se puede leer?
try
{
Atlas.GetPixel(0, 0);
}
catch(UnityException e)
{
if(e.Message.StartsWith("Texture '" + Atlas.name + "' is not readable"))
{
//No se puede leer, avisamos y preguntamos si convertimos o cancelamos
bool Convertir = EditorUtility.DisplayDialog ("Error", "La imagene tiene que estar habilitado 'Read/Write Enabled'", "Convertir", "Cancelar");
if (Convertir)
{
//Lo hacemos que se puede leer
TextureImporter importer = AssetImporter.GetAtPath (path) as TextureImporter;
importer.isReadable = true;
AssetDatabase.ImportAsset (path);
}
else
{
//Cancelo
return;
}
}
}
//Tomamos todos los sprites
Object[] atlasAssets = AssetDatabase.LoadAllAssetsAtPath(path);
//Convertimos a png y guardamos
float progressActual = 0;
foreach (Object asset in atlasAssets)
{
EditorUtility.DisplayProgressBar("Converting", "Converting Sprites to Textures", progressActual / atlasAssets.LongLength);
if (AssetDatabase.IsSubAsset(asset))
{
//Convertimos
Texture2D img = ConvertSpriteToTexture (asset as Sprite);
//Generamos PNG
byte[] pngData = img.EncodeToPNG();
if (pngData != null)
{
//Guardamos imagen
File.WriteAllBytes(path + asset.name + ".png", pngData);
}
}
progressActual++;
}
if (progressActual != 0)
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh();
this.Close();
}
//Ayuda obtener la carpeta
string GetFolder(string _path)
{
return _path.Remove(_path.LastIndexOf("/"));
}
//Covnierte el sprite a textura
static Texture2D ConvertSpriteToTexture(Sprite sprite)
{
try
{
if (sprite.rect.width != sprite.texture.width)
{
Texture2D newText = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
Color[] newColors = sprite.texture.GetPixels((int)System.Math.Ceiling(sprite.textureRect.x),
(int)System.Math.Ceiling(sprite.textureRect.y),
(int)System.Math.Ceiling(sprite.textureRect.width),
(int)System.Math.Ceiling(sprite.textureRect.height));
newText.SetPixels(newColors);
newText.Apply();
return newText;
}
else
return sprite.texture;
}catch
{
return sprite.texture;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment