2D Sprites : Slice multiple sprite sheets at once
I have about 150 sprite sheets I need to slice. Each should be split by grid into 64 x 64 cells. It is an task in the Sprite Editor, except for the fact I need to do this 150 times. Is there a way to shift+click multiple sprite sheets then slice all at once? I can't seem to open the Sprite Editor with multiple sheets clicked.
nope sorry you will have to do this manually for each sprite sheet. Though i don't know if there is an editor extension you can purchase that will accomplish this.
Thats what I was worried about. I just clicked through it all - pretty tedious, but didn't take as long as I thought. I'd hate to do this for 1000+ sheets!
lol yeah sometimes it just comes with the territory
Answer by lucks17 · Dec 23, 2019 at 04:23 AM
Maybe this could help: https://answers.unity.com/questions/1113025/batch-operation-to-slice-sprites-in-editor.html?_ga=2.32794360.796740819.1577073889-1730206695.1568833357
Answer by FreeReignPublishing · Feb 05 at 07:12 AM
here's an overhaul to the script...
Added a seperate window under the menu item for slicing
Can now auto slice a sprite ie "can cut out nothing but the sprite from dead space"
To slice a sprite select one or more in the asset hierarchy then click the slice button in the window
Can change the pivot for each sprite sliced
Can change the threshold ie "the max pixel opacity to check for when auto slicing"
Make sure to put this script in a folder called: Editor
using UnityEditor;
using System.Collections.Generic;
using UnityEngine;
public class SpriteSlicer : EditorWindow
{
static float threshold = 1;
static Vector2 pivot = new Vector2(0.5f, 0.5f);
void OnGUI()
{
threshold = Mathf.Clamp(EditorGUILayout.FloatField("Threshold: ", threshold), 0, 1);
pivot = EditorGUILayout.Vector2Field("Pivot: ", pivot);
if (GUILayout.Button("Slice"))
{
Slice();
}
this.Repaint();
}
[MenuItem("Sprite Slicer/Open Slicer")]
static void OpenSlicer()
{
EditorWindow.GetWindow(typeof(SpriteSlicer));
}
static void Slice()
{
Object[] spriteSheets = Selection.GetFiltered<Texture2D>(SelectionMode.Assets);
for (int z = 0; z < spriteSheets.Length; z++)
{
string path = AssetDatabase.GetAssetPath(spriteSheets[z]);
TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
ti.isReadable = true;
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
if (ti.spriteImportMode != SpriteImportMode.Single)
ti.spriteImportMode = SpriteImportMode.Single;
ti.spriteImportMode = SpriteImportMode.Multiple;
List<SpriteMetaData> newData = new List<SpriteMetaData>();
Texture2D spriteSheet = spriteSheets[z] as Texture2D;
int maxY = 0;
int maxX = 0;
int minY = spriteSheet.height;
int minX = spriteSheet.width;
for (int i = 0; i < spriteSheet.width; i++)
{
for (int j = spriteSheet.height; j >= 0; j--)
{
if (spriteSheet.GetPixel(i, j).a >= threshold && j > maxY)
maxY = j;
if (spriteSheet.GetPixel(i, j).a >= threshold && j < minY)
minY = j;
if (spriteSheet.GetPixel(i, j).a >= threshold && i > maxX)
maxX = i;
if (spriteSheet.GetPixel(i, j).a >= threshold && i < minX)
minX = i;
}
}
SpriteMetaData smd = new SpriteMetaData();
smd.pivot = pivot;
smd.alignment = 9;
smd.name = spriteSheet.name;
smd.rect = new Rect(minX, minY, (maxX - minX) + 1, (maxY - minY) + 1);
newData.Add(smd);
ti.spritesheet = newData.ToArray();
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
}
}
}