Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6.  
  7. public class SpriteToTexture : EditorWindow
  8. {
  9. private Texture2D Atlas;
  10.  
  11. [MenuItem("Window/SpriteToTexture")]
  12. public static void Init()
  13. {
  14. EditorWindow.GetWindow(typeof(SpriteToTexture), true, "Sprite To Texture");
  15. }
  16.  
  17. void OnGUI()
  18. {
  19. Atlas = (Texture2D)EditorGUILayout.ObjectField("Atlas", Atlas, typeof(Texture2D), false);
  20.  
  21. if (Atlas) //Ya se asigno una textura?
  22. {
  23. if (GUILayout.Button("Convert"))
  24. {
  25. ConvertAtlas2Prefabs();
  26. }
  27. }
  28. }
  29.  
  30. void ConvertAtlas2Prefabs()
  31. {
  32. //Tomamos todos los sprites
  33. string path= AssetDatabase.GetAssetPath(Atlas);
  34. Object[] atlasAssets = AssetDatabase.LoadAllAssetsAtPath(path);
  35.  
  36.  
  37. //Convertimos a png y guardamos
  38. float progressActual = 0;
  39. foreach (Object asset in atlasAssets)
  40. {
  41. EditorUtility.DisplayProgressBar("Converting", "Converting Sprites to Textures", progressActual / atlasAssets.LongLength);
  42. if (AssetDatabase.IsSubAsset(asset))
  43. {
  44. //Convertimos
  45. Texture2D img = ConvertSpriteToTexture (asset as Sprite);
  46. //Generamos PNG
  47. byte[] pngData = img.EncodeToPNG();
  48. if (pngData != null)
  49. {
  50. //Guardamos imagen
  51. File.WriteAllBytes(path + asset.name + ".png", pngData);
  52. }
  53. }
  54. progressActual++;
  55. }
  56. if (progressActual != 0)
  57. EditorUtility.ClearProgressBar();
  58.  
  59. AssetDatabase.Refresh();
  60. this.Close();
  61. }
  62.  
  63. //Ayuda obtener la carpeta
  64. string GetFolder(string _path)
  65. {
  66. return _path.Remove(_path.LastIndexOf("/"));
  67. }
  68.  
  69. //Covnierte el sprite a textura
  70. static Texture2D ConvertSpriteToTexture(Sprite sprite)
  71. {
  72. try
  73. {
  74. if (sprite.rect.width != sprite.texture.width)
  75. {
  76. Texture2D newText = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
  77. Color[] colors = newText.GetPixels();
  78. Color[] newColors = sprite.texture.GetPixels((int)System.Math.Ceiling(sprite.textureRect.x),
  79. (int)System.Math.Ceiling(sprite.textureRect.y),
  80. (int)System.Math.Ceiling(sprite.textureRect.width),
  81. (int)System.Math.Ceiling(sprite.textureRect.height));
  82. Debug.Log(colors.Length+"_"+ newColors.Length);
  83. newText.SetPixels(newColors);
  84. newText.Apply();
  85. return newText;
  86. }
  87. else
  88. return sprite.texture;
  89. }catch
  90. {
  91. return sprite.texture;
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement