Creating UI Atlases in Photoshop Automagically
A little over a year ago I was working on a game engine for a successful toy company. The project never ended up being finished (long, nasty story which I'll happily tell over beers), but one of the interesting things I did for this project was build a Photoshop-to-Unity automatic UI workflow. The basic idea was:
- Create UI layout in Photoshop, with one "root level" layer or layer group corresponding to a control.
- Name the groups according to a fairly complicated naming convention (which encapsulated both behavior and functionality, e.g. how a button might change its appearance when hovered or clicked and what clicking it would do).
- Press a button.
- Select a target folder (which could be inside a Unity project's "Resources" folder, of course).
- And point a script at the folder.
Another obvious problem was the embedding of behavior in names — it was convenient if you got it right the first time, but a serious pain in the ass for iterative development (either change the name in Photoshop and re-export everything or change the name in Photoshop and then edit the metadata file, and… yuck).
Anyway, I've had the "perfect" successor bouncing around in my head for a while and then the other day it struck me that someone probably has written a Photoshop image atlas tool already, and I might be able to rip that off and integrate it with my script.
Turns out that (a) someone has written an image atlas tool for Photoshop and (b) that the key component of that tool was a rectangle packer someone else (sadly the link is defunct) had written, implementing an algorithm documented here.
So that's what I spent New Year's Eve doing, and the result —
Finally, the script creates and saves the atlas itself (as a PNG file, of course).
Aside from the CSS sprite support I mention in the comments in a TODO — an obvious thing for this tool to be able to do would be to export a bunch of CSS styles allowing the images in the atlas to be used as CSS sprites — there's one more missing thing: a Unity UI library that consumes the JSON/atlas combination and produces a UI.
That's my project for tonight.