When we started making Academia, we didn’t really plan out how are we going to manage our sprites. We just did the quickest way which was to make them individually and load them in the game. All of our game world sprites are stored in StreamingAssets. We load them dynamically when the game is run. This is different from the normal way using an imported texture. We did it this way in preparation for modding support. I envision that modders could then add their own folders and provide them the mechanism to override the base game images.

Ideally, all game sprites should be in a single big texture. This will allow you to use a common material throughout your game objects so that dynamic batching can indeed batch. Now that the game got bigger, it’s harder to put all of our sprites in one single atlas. Our artist wouldn’t agree to this as it’s a lot of work. Additionally, we no longer have the time. We’re releasing our Early Access this September 8.

While coming up with solutions, I thought what if I could pack the images dynamically instead and use the generated atlas. It should be simple enough to recompute the UVs of the packed sprites. I scrounged the internet on algorithms on how to optimally pack rectangles in a bigger one. Turns out that this is an interesting problem. There are numerous papers about this. It also turned out that I no longer have to roll up my own packer. Unity already made one.

It needs some help, though. I needed something that keeps track of the packed textures. I needed a way to get the same sprite out of the packed one. Time to code!

Here’s the class that abstracts an “entry” of a packed texture:

public class PackedTextureEntry { private readonly Texture2D atlas; private readonly Rect uvRect; private readonly Rect spriteRect; private readonly Sprite sprite; public PackedTextureEntry(Texture2D atlas, Rect uvRect) { this.atlas = atlas; this.uvRect = uvRect; this.spriteRect = new Rect(this.uvRect.x * this.atlas.width, this.uvRect.y * this.atlas.height, this.uvRect.width * this.atlas.width, this.uvRect.height * this.atlas.height); this.sprite = Sprite.Create(this.atlas, this.spriteRect, new Vector2(0.5f, 0.5f), 768); } public Texture2D Atlas { get { return atlas; } } public Rect UvRect { get { return uvRect; } } public Rect SpriteRect { get { return spriteRect; } } public Sprite Sprite { get { return sprite; } } public Sprite CreateSprite(Vector2 pivot) { return Sprite.Create(this.atlas, this.spriteRect, pivot, 768); } }

Basically, it’s just a container of the generated atlas and the UV coordinates of a particular sprite entry. The Rect passed in the constructor is a normalized UV (values are zero to one). Sprites, however, are created using pixels. So we need a new Rect for this which is just the UV rect multiplied by the dimensions of the atlas. This class also has a pre-generated Sprite pivoted at the center.

The following class is the texture packer itself:

public class TexturePacker { // Contains the associated names of the added texture so we can easily query its entry after packing private List<string> names = new List<string>(); // This contains the textures to pack // Used a list here so we could easily convert to array during packing private List<Texture2D> textures = new List<Texture2D>(); // Keeps track of the packed entries private Dictionary<string, PackedTextureEntry> entriesMap = new Dictionary<string, PackedTextureEntry>(); private Texture2D atlas; public TexturePacker() { } public void Add(string key, Texture2D texture) { this.names.Add(key); this.textures.Add(texture); } public void Pack() { this.atlas = new Texture2D(2, 2, TextureFormat.ARGB32, false); // Will expand on packing Rect[] rects = this.atlas.PackTextures(this.textures.ToArray(), 0, 8192, true); // Populate entries this.entriesMap.Clear(); Assertion.Assert(this.names.Count == this.textures.Count); for(int i = 0; i < this.names.Count; ++i) { this.entriesMap[this.names[i]] = new PackedTextureEntry(this.atlas, rects[i]); } // Clear to save memory // These textures may also be released this.textures.Clear(); } public PackedTextureEntry GetEntry(string key) { return this.entriesMap[key]; } }

Usage is self explanatory. Create an instance of the packer. Add the textures that you want to pack, each associated with a string key. Call Pack(). Use GetEntry() to get an instance of PackedTextureEntry associated with the sprite. Use PackedTextureEntry.Sprite property to have access of the sprite that is from the packed texture.

TexturePacker packer = new TexturePacker(); // Let's just say you have a library of textures associated by name foreach(TextureEntry entry in entries) { packer.Add(entry.Name, entry.Texture); } packer.Pack(); // Get a packed entry and use its sprite PackedTextureEntry packedEntry = packer.Get("Grass"); spriteRenderer.sprite = packedEntry.Sprite;

And that’s it! It’s really simple but this thing helped in batching by a lot.