Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEditorInternal;
- using UnityEngine;
- namespace ManaSeed.Editor
- {
- public class SpritesheetSlicer
- {
- public static Vector2 ManaSeedCharacterPivot = new Vector2(0.5f, 0.3125f);
- [MenuItem("Mana Seed/Slice Character Sprites %&s")]
- public static void SliceManaSeedCharacters()
- {
- var textures = Selection.GetFiltered<Texture2D>(SelectionMode.Assets);
- foreach (var texture in textures)
- ProcessManaSeedCharacterTexture(texture);
- Debug.Log("Slicing operation completed.");
- }
- private static void ProcessManaSeedCharacterTexture(Texture2D texture)
- {
- string path = AssetDatabase.GetAssetPath(texture);
- var importer = AssetImporter.GetAtPath(path) as TextureImporter;
- importer.textureType = TextureImporterType.Sprite;
- importer.spriteImportMode = SpriteImportMode.Multiple;
- importer.mipmapEnabled = false;
- importer.spritePivot = ManaSeedCharacterPivot;
- var textureSettings = new TextureImporterSettings();
- importer.ReadTextureSettings(textureSettings);
- textureSettings.spriteMeshType = SpriteMeshType.Tight;
- textureSettings.spriteExtrude = 0;
- importer.SetTextureSettings(textureSettings);
- EditorUtility.SetDirty(importer);
- Rect[] rects = InternalSpriteUtility.GenerateGridSpriteRectangles(texture, Vector2.zero, new Vector2(64, 64), Vector2.zero, true);
- var rectsList = new List<Rect>(rects);
- rectsList = SortRects(rectsList, texture.width);
- string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
- var metas = new List<SpriteMetaData>();
- int rectNum = 0;
- foreach (Rect rect in rectsList)
- {
- var meta = new SpriteMetaData();
- meta.pivot = ManaSeedCharacterPivot;
- meta.alignment = (int)SpriteAlignment.Custom;
- meta.rect = rect;
- meta.name = filenameNoExtension + "_" + rectNum++;
- metas.Add(meta);
- }
- importer.spritesheet = metas.ToArray();
- AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
- Debug.Log("Slicing spritesheet " + texture.name + " completed.");
- }
- private static List<Rect> SortRects(List<Rect> rects, float textureWidth)
- {
- List<Rect> list = new List<Rect>();
- while (rects.Count > 0)
- {
- Rect rect = rects[rects.Count - 1];
- Rect sweepRect = new Rect(0f, rect.yMin, textureWidth, rect.height);
- List<Rect> list2 = RectSweep(rects, sweepRect);
- if (list2.Count <= 0)
- {
- list.AddRange(rects);
- break;
- }
- list.AddRange(list2);
- }
- return list;
- }
- private static List<Rect> RectSweep(List<Rect> rects, Rect sweepRect)
- {
- List<Rect> result;
- if (rects == null || rects.Count == 0)
- {
- result = new List<Rect>();
- }
- else
- {
- List<Rect> list = new List<Rect>();
- foreach (Rect current in rects)
- {
- if (current.Overlaps(sweepRect))
- {
- list.Add(current);
- }
- }
- foreach (Rect current2 in list)
- {
- rects.Remove(current2);
- }
- list.Sort((a, b) => a.x.CompareTo(b.x));
- result = list;
- }
- return result;
- }
- }
- }
Add Comment
Please, Sign In to add comment