Home › Page › Tutorials – The Sprite Atlas & Nine-Slicing

Tutorials – The Sprite Atlas & Nine-Slicing

Mini Tutorials > Tutorials - The Sprite Atlas & Nine-Slicing

The Sprite Atlas & Definition

In the last two tutorials, we used Sprite Definitions without really knowing what they were or how to make them. Well, now we are going to learn two ways to make them from any image file or files, so that you can use your own art assets as UghSprites in the Ugh system.

You can download the project for this tutorial here.

Part 1 – Creating a Sprite Definition

You can either follow along with the video, or step through the written tutorial below.

Here we will create a Sprite Definition for any arbitrary image file you want to use with your Ugh elements.

  1. Import any image you would like to use for a button or sprite into the project. Highlight this image.
  2. Right-click on the image and select “Ugh->Generate Ugh Sprite Definition From Selected Textures”
  3. Watch as a sprite definition is created for you in the project view! It may take a few minutes, depending on how big the image is and how fast your computer is. Note: Sprite Definitions can never exist in the hierarchy view. They are only used by UghSprite type objects, never seen on their own as GameObjects.

Part 2 – Creating a Sprite Atlas

If you have a lot of individually-created Sprite Definitions in your scene, you are going to have a lot of separate materials and, therefore, a lot of draw calls. To cut down on the number of draw calls and organize all your Sprite Definitions, you can combine multiple images into a single material. This is how you do it.

  1. Create a folder in the Project view and put all the image files that you want Sprite Definitions for into it. All of the images in this folder will be put onto a single Sprite Atlas.  (Note that a Sprite Atlas generally only holds up to 1024 x 1024 pixels of information.  If you have more images than will fit in this space, separating the images into multiple Sprite Atlases.)
  2. Highlight the folder you wish to atlas and then right-click and select “Ugh->Generate Ugh Sprite Atlas From Folder”
  3. Watch as a Sprite Atlas is created.

Note that the file structure for the created Sprite Definitions and related files is the same whether you create a Sprite Definition from a single texture or from a folder. The SpriteAtlas folder contains one Sprite Atlas image file for each separate resolution you intend to support (and a material for each,) the Settings file for the Sprite Atlas, the folder for the Sprite Definitions, and the folder for the Sprite Prototypes that the Sprite Definitions make use of. For now, remember the Sprite Definitions folder, you will use this most often.

Part 3 – Advanced: SpriteAtlas Settings files

If Parts 1 and 2 left you with some Sprite Definitions and Atlases that you are happy with, you don’t need this part. This part is for people who want to really try to get the most out of the customization and functionality that UGH Sprite Atlasing offers.

When a SpriteAtlas is generated, so is a SpriteAtlas Settings file. If you are building a brand new Sprite Atlas, the SpriteAtlas Settings file will take its configuration from the DefaultSpriteAtlasSettings file included with Ugh, which should be placed in the main Assets folder. If you are rebuilding an already existing Sprite Atlas — by right-clicking a folder which you already generated a sprite atlas from and selecting “Generate Ugh Sprite Atlas from Folder” — the SpriteAtlas Settings will be preserved for the rebuilt Sprite Atlas. This is how you can use the advanced multi-resolution or custom resolution features of Ugh Sprite Atlases: you must first create (or copy) a SpriteAtlas Settings file under the SpriteAtlas folder and change it before building or rebuilding the SpriteAtlas from the textures.

Now that you know where the SpriteAtlas Settings file is and generally how to work with it, let’s look at the contents of the file. The file is edited by clicking on it and changing options in the Inspector view. Here’s some of the things you can change and what they do:

  • Resolutions – Ugh Sprite Atlases support atlasing to multiple resolutions at once. That means that, on lower end devices, only the smaller resolution sprite atlases will be loaded, saving valuable real time memory. Note that all sprite atlases in all resolutions included in your game or project are packaged with the download, but only the appropriately sized sprite atlases are loaded into real time memory. In order to build a Sprite Atlas to multiple resolutions, change the size of the Resolutions array to more than one. For each element in the array, you can specify:
    • Name: this is for your reference only
    • Min X, Min Y: specify the minimum x and y values the sprite atlas will use
    • Max Atlas Texture Size: This determines the max, and usually the actual, size of the Sprite Atlas. This must be a power of 2! (512, 1024, etc)
    • Auto Sharpen: auto sharpens the Sprite Atlas image, if checked
  • Shader: the default is usually fine for this. If you understand shaders, feel free to change this string to match the name and directory of the shader you would like to use for that Sprite Atlas. We recommend Unlit shaders, but you can use any kind.
  • Include Alpha Channel: If checked, includes the alpha channels for each texture that is atlased
  • Padding: This is how many pixels are placed in between textures on the atlas, in order to reduce bleeding from one texture into another. We set this by default to 1, which has been fine for us, but you can set it higher than that if bleeding is an issue for your Sprite Atlas, or to 0 if the texture(s) you are atlasing fit very snugly into your desired atlas size. Be careful about setting Padding to 0, though, and watch out for texture bleeding.

Nine-Slicing Scaling

A very powerful feature of Ugh is the ability to use nine-slice scaling. If you’ve never heard of nine-slice scaling or aren’t sure what it is, there are plenty of good articles explaining the general concept.  (Try this one or this one.)

  1. Find and highlight the Sprite Definition you’d like to apply a nine-slice to.
  2. In the Inspector view, open up the variable lists called “Pixel Crops” and “Pixel Insets.”
    • “Pixel Crops” tells the Sprite Definition how much to cut off of the edges of the sprite. This is a standard cropping tool and not part of a “nine-slicing” operation.  It is useful for getting rid of any artifacts created by PVRTC compression.
    • “Pixel Insets” defines the inset distance between each edge and the corresponding cut to be made when nine-slicing this sprite.
  3. Once these numbers are set, click “Apply 9-Slice.”
  4. If you have an UghSprite in the scene that uses the Sprite Definition you are editing, you will be able to see the changes immediately if it was previously skewed by a lack of nine-slice scaling. You can test values live like this, changing the numbers, applying your changes, and seeing the effect on your sprite live in the scene or game view.

Got it? Well done! You now know how to import your own graphics into the Ugh system.