Skip to content

Instantly share code, notes, and snippets.

@cmaher
Last active January 18, 2021 18:58
Show Gist options
  • Save cmaher/2b8f8ca3cbc95461c6010ad6dd45d97c to your computer and use it in GitHub Desktop.
Save cmaher/2b8f8ca3cbc95461c6010ad6dd45d97c to your computer and use it in GitHub Desktop.
Unity Batch Sprite Slicer
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// Put this script in the Assets/Editor folder
namespace MaruEditor.Sprites {
// Select a bunch of sprites, and slice them all at once
public class BatchSpriteSlicer : EditorWindow {
[MenuItem("Maru/Slice Selected Sprites")]
static void Init() {
var window = CreateInstance<BatchSpriteSlicer>();
window.Show();
}
private static Vector2Int lastSize;
private Rect buttonRect;
private Vector2Int sliceSize;
private bool sliceClicked;
private bool cancelClicked;
void OnGUI() {
if (sliceSize == Vector2Int.zero) {
sliceSize = lastSize;
}
sliceSize = EditorGUILayout.Vector2IntField("Slice Size", sliceSize);
lastSize = sliceSize;
sliceClicked = GUILayout.Button("Slice");
cancelClicked = GUILayout.Button("Cancel");
if (sliceClicked) {
if (sliceSize.x == 0 || sliceSize.y == 0) {
Debug.LogError("Must set Slice Size to non-zero values");
return;
}
SliceSprites();
Debug.Log("Done Slicing!");
Close();
}
if (cancelClicked) {
Close();
}
}
// adapted from https://answers.unity.com/questions/1113025/batch-operation-to-slice-sprites-in-editor.html
void SliceSprites() {
foreach (var si in Selection.objects) {
var tex = si as Texture2D;
var path = AssetDatabase.GetAssetPath(tex);
var ti = AssetImporter.GetAtPath(path) as TextureImporter;
if (ReferenceEquals(ti, null)) {
continue;
}
ti.isReadable = true;
ti.spriteImportMode = SpriteImportMode.Multiple;
var newData = new List<SpriteMetaData>();
for (var i = 0; i < tex.width; i += sliceSize.x) {
for (var j = tex.height; j > 0; j -= sliceSize.y) {
var smd = new SpriteMetaData {
pivot = new Vector2(0.5f, 0.5f),
alignment = 9,
name = tex.name + "_" + newData.Count,
rect = new Rect(i, j - sliceSize.y, sliceSize.x, sliceSize.y),
};
newData.Add(smd);
}
}
ti.spritesheet = newData.ToArray();
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment