Creating palette from irregular sprites in Unity

I want to create a palette based on a number of PNGs in Unity 2D. I am struggling to understand the slicing logic. Let's say I have a sprite 588x547, and I set PPU to 128. So obviously the sprite cannot be divided into 128-parts: So the right and the bottom part are not going to appear on my palette. Now I would like the grid to start in the center. The tiles on the sides are not "full" tiles, but I still want them on my palette in the end, so I kind of want Unity to fill up the border tiles for me with void till they reach the 128x128 size: Is there a way to achieve this? Or am I conceptually misunderstanding something? Given a bunch of PNGs, all of them different pixel sizes, none dividable by 128, how do you create a palette from them to paint on a 128 PPU grid? I had a look at external tools like TexturePacker and Showbox, but did't manage to achieve a nice 128-dividable grid from my sprites.

• It seems to me that you could do this by dragging out your desired sprite rectangles manually, and editing the numeric fields to snap them to your exact 128 stride target. Have you had any trouble making that work? Aug 26 '19 at 21:50
• What do you mean by "dragging out"? In the Unity slicing editor? Or in an external app? My last resort would be loading it all into Inkscape and puting sprites into right places on a 128 grid, then loading the sheet into Unity and slicing it. But I was hoping there might be an automatic way.. Aug 27 '19 at 6:47
• Yes, Unity lets you draw whatever bounding rectangles you want for your sprites inside its own editor. Aug 27 '19 at 11:27

The easiest way to do this by using a tile palette or just resizing the image by not cutting it up. So you can change the image to Point (No filter) instead of Bilinear. After that you choose (Grid by cell size). Then you get a series of cut p parts (Also, you should probably choose a power of 2 or a multiple of two). Then click create, 2d, tile map. Then, you can go to Window, 2d, and Tile Palette. Click create tile palette and select the series of images that you want to be to create the image. Then you can place the correct part at the correct place. Hope this helps! (also, you can use the original image and create a box collider if that's what you want. you can do that by just choosing Automatic instead of Cell by Grid size). Let me know if you want more help.

• can someone please upvote my answer, I would really appreciate it! May 27 '20 at 0:32

Well, I could not figure it out with Unity, so I ended up resizing all my sprites to 128-divisible sizes by adding transparent borders, so that to be able to cut them with Unity nicely. For this I used ImageMagic and the following command:

convert {file} -background transparent -gravity center -extent {targetW}x{targetH} {outputPath}


In order to go through all the files in the directory and calculate the next best 128-divisible size I wrote a small app in C#:

class Program
{
static void Main(string[] args)
{
var sourcePath = @"d:\png";
var targetPath = @"d:\png\output";

var files = Directory.GetFiles(sourcePath);
foreach (var file in files)
{
var img = Image.FromFile(file);
var w = img.Width;
var h = img.Height;
var targetW = FindNextSize(w);
var targetH = FindNextSize(h);

Process.Start(
@"...path to ImageMagic...\convert.exe",
\$@"{file} -background transparent -gravity center -extent {targetW}x{targetH} {Path.Combine(targetPath, Path.GetFileName(file))}");
}
}

private static int step = 128;

private static int FindNextSize(int actualSize)
{
var result = step;
while (result <= actualSize)
{
result += step;
}

return result;
}
}


Not too pretty all of it, but it did what I wanted.