Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
- {
- //Tomamos todos los sprites
- string path= AssetDatabase.GetAssetPath(Atlas);
- 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[] colors = newText.GetPixels();
- 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));
- Debug.Log(colors.Length+"_"+ newColors.Length);
- newText.SetPixels(newColors);
- newText.Apply();
- return newText;
- }
- else
- return sprite.texture;
- }catch
- {
- return sprite.texture;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement