Why you should use optimized sprites
I guess that you already know that sprite sheets increase the performance of your game. But with TexturePacker you can do even more.
Unity creates sprite meshes for you — they reduce the amount of overdraw by simply not drawing transparent pixels. The only problem is the quality of the generated mesh:
TexturePacker comes with a new algorithm to create better meshes. As you see above:
- Triangle count reduced by 69% — giving you additional CPU power
- Overdraw reduced by 30% — giving you additional GPU power
What you are going to learn in this tutorial
TexturePacker, together with the free TexturePackerImporter, creates sprite sheet assets that can be directly used from Unity's graphical editor or through scripts.
Sprites are accessible through Unity's standard API using the Sprite class — no additional runtime code required! The sprite sheets also work with Unity's UI classes but don't support the optimized sprite meshes..
This is what you get:
- Increased performance through optimized sprite meshes
- Easy sprite management
- 1-click import/export
- No additional runtime code or library required
- Fully automated slicing of sprites
- Saves you lot of time
Creating sprite sheets
To create a new sprite sheet, simply start TexturePacker and drag & drop the directories containing your sprites to the Sprites area. TexturePacker preserves the directory structure under the directory you added - allowing you to group and sort sprites. New sprites in the directory are added to the sheet as soon as you re-enter TexturePacker.
Select the Data format Unity - Texture2d from the settings on the right side.
Click on the folder icon next to Data file name and choose a location in the Assets directory of your Unity project — the TexturePackerImporter script will re-import the sprite sheet into your project each time you publish an updated sheet with TexturePacker.
TexturePacker 4's default packing Algorithm is Polygon — which is good for sprites. It does not work with UI images — since they only work with rectangles — overlapping sprites would give you artifacts. In this case use MaxRects. You could also try MaxRects when packing polygon sprites, it might deliver better packing results depending on your sprite data.
- Increase the Tracer Tolerance — this will create less compex polygons
- Use MaxRects — TexturePacker uses rectangles for packing but still uses the optimized meshes
Finally press Publish to create the sprite sheet.
Optimizing polygon meshes
You can influence the quality of the meshes using Tracer Tolerance. A higher tolerance results in less vertices but more overdraw.
The best value for your project depends on 1 factors: Your CPU usage and GPU usage. Each vertex requires CPU power for the calculation. On the other hand: Each additional pixel adds work load on the GPU.
If your project is high on computation (CPU) but you still have some resources on the GPU side go for a lower vertex count - increase tracer tolerance.
If your project is high on graphics (CPU) but you still have some resources on the CPU side go for a lower overdraw - reduce tracer tolerance.
Keep an eye on the values TexturePacker is displaying at the bottom of the sprite sheet. In the following example you see that decreasing the overdraw by additional 2% comes with more than duplicating the vertex count.
To enable the import of TexturePacker sprite sheets in Unity, you have to install TexturePacker Importer, which you can download from the Unity Asset Store for free:
The script extends Unity to read sprite sheet data created with TexturePacker and to automatically create native Unity assets which you can use with the Unity Editor. The code of the script only runs during development. It does not become part of your product or game.
Import this package into your Unity project by opening the link from above in Unity:
The plugin comes with some demo assets — but it's perfect if you only install the TexturePackerImporter.dll.
It is important that the dll is located in a folder called Editor to ensure that it is automatically loaded by the Unity Editor.
The TexturePackerImporter checks for each texture file if a corresponding .tpsheet file exists. This is the data file written by TexturePacker — it is used by the Unity importer to slice the texture into individual sprites.
Use the symbol next to the texture item to display the individual sprites. To create an animation simply select the frames and drag them onto the scene.
Now you can use your sprites as usual, directly from the editor.
You can, of course, also access the sprite through the scripting API:
Pivot point editor
TexturePacker contains an easy to use pivot point editor with real time animation preview.
Select your sprites in the left panel and click on Sprite settings in the top toolbar. Pivot points are represented by a circle. You can select multiple shapes and move all pivot points at once.
Use Anim preview from the top toolbar to see an animation of the selected sprites. Changes to the pivot points are updated while you edit them.
The right panel contains edits to set the pivot points in absolute and relative coordinates. The relative coordinates use the range of 0 — 1 where 0/0 is in the bottom left of the sprite.
The Predefined combo-box let you choose from the standard positions in the corners and center of the sprite.
You can also set the default values for new sprites by clicking the arrow button on the right panel.
Border editor (9-patch, 3-patch)
TexturePacker also supports editing of sprite borders — also know as 9-patch or 3-patch sprites.
You can use this feature to create scalable buttons, bars and other objects with a defined stretch.
A sprite is cut into 3 (x or y direction) or 9 parts (x and y direction). The parts of the sprite in the corners are fixed. The others stretch when the sprite is resized.
You can enable the border setting for individual sprites from the Sprite settings in the top toolbar.
Drag the green lines to separate the 9 segments for a 9-patch sprite. Move the unused lines to the top/bottom or left/right to create 3-patch sprites.
The UI classes of Unity ignore the polygon mesh information and simply use rectangular sprites. Since the UI also does not use the pivot points it's recommended to disable trimming, too:
- Algorithm = MaxRects
- Trim mode = None
TexturePacker simplifies the workflow for your sprites in Unity - automating the whole process.
Changing your sprites is quite simple:
- Save the new sprites in your assets folder
- Press Publish in TexturePacker
- Switch to Unity and use your sprites