DirePixel

Spritesheet Slicer

Mar 25th, 2021 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEditorInternal;
  5. using UnityEngine;
  6.  
  7. namespace ManaSeed.Editor
  8. {
  9.     public class SpritesheetSlicer
  10.     {
  11.         public static Vector2 ManaSeedCharacterPivot = new Vector2(0.5f, 0.3125f);
  12.  
  13.         [MenuItem("Mana Seed/Slice Character Sprites %&s")]
  14.         public static void SliceManaSeedCharacters()
  15.         {
  16.             var textures = Selection.GetFiltered<Texture2D>(SelectionMode.Assets);
  17.  
  18.             foreach (var texture in textures)
  19.                 ProcessManaSeedCharacterTexture(texture);
  20.  
  21.             Debug.Log("Slicing operation completed.");
  22.         }
  23.  
  24.         private static void ProcessManaSeedCharacterTexture(Texture2D texture)
  25.         {
  26.             string path = AssetDatabase.GetAssetPath(texture);
  27.             var importer = AssetImporter.GetAtPath(path) as TextureImporter;
  28.  
  29.             importer.textureType = TextureImporterType.Sprite;
  30.             importer.spriteImportMode = SpriteImportMode.Multiple;
  31.             importer.mipmapEnabled = false;
  32.             importer.spritePivot = ManaSeedCharacterPivot;
  33.  
  34.             var textureSettings = new TextureImporterSettings();
  35.             importer.ReadTextureSettings(textureSettings);
  36.             textureSettings.spriteMeshType = SpriteMeshType.Tight;
  37.             textureSettings.spriteExtrude = 0;
  38.             importer.SetTextureSettings(textureSettings);
  39.  
  40.             EditorUtility.SetDirty(importer);
  41.  
  42.             Rect[] rects = InternalSpriteUtility.GenerateGridSpriteRectangles(texture, Vector2.zero, new Vector2(64, 64), Vector2.zero, true);
  43.             var rectsList = new List<Rect>(rects);
  44.             rectsList = SortRects(rectsList, texture.width);
  45.  
  46.             string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
  47.             var metas = new List<SpriteMetaData>();
  48.             int rectNum = 0;
  49.  
  50.             foreach (Rect rect in rectsList)
  51.             {
  52.                 var meta = new SpriteMetaData();
  53.                 meta.pivot = ManaSeedCharacterPivot;
  54.                 meta.alignment = (int)SpriteAlignment.Custom;
  55.                 meta.rect = rect;
  56.                 meta.name = filenameNoExtension + "_" + rectNum++;
  57.                 metas.Add(meta);
  58.             }
  59.  
  60.             importer.spritesheet = metas.ToArray();
  61.  
  62.             AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
  63.  
  64.             Debug.Log("Slicing spritesheet " + texture.name + " completed.");
  65.         }
  66.  
  67.         private static List<Rect> SortRects(List<Rect> rects, float textureWidth)
  68.         {
  69.             List<Rect> list = new List<Rect>();
  70.             while (rects.Count > 0)
  71.             {
  72.                 Rect rect = rects[rects.Count - 1];
  73.                 Rect sweepRect = new Rect(0f, rect.yMin, textureWidth, rect.height);
  74.                 List<Rect> list2 = RectSweep(rects, sweepRect);
  75.                 if (list2.Count <= 0)
  76.                 {
  77.                     list.AddRange(rects);
  78.                     break;
  79.                 }
  80.                 list.AddRange(list2);
  81.             }
  82.             return list;
  83.         }
  84.  
  85.         private static List<Rect> RectSweep(List<Rect> rects, Rect sweepRect)
  86.         {
  87.             List<Rect> result;
  88.             if (rects == null || rects.Count == 0)
  89.             {
  90.                 result = new List<Rect>();
  91.             }
  92.             else
  93.             {
  94.                 List<Rect> list = new List<Rect>();
  95.                 foreach (Rect current in rects)
  96.                 {
  97.                     if (current.Overlaps(sweepRect))
  98.                     {
  99.                         list.Add(current);
  100.                     }
  101.                 }
  102.                 foreach (Rect current2 in list)
  103.                 {
  104.                     rects.Remove(current2);
  105.                 }
  106.                 list.Sort((a, b) => a.x.CompareTo(b.x));
  107.                 result = list;
  108.             }
  109.             return result;
  110.         }
  111.     }
  112. }
Add Comment
Please, Sign In to add comment