How to use sprites in Unity?
What is a Sprite in Unity?
A Sprite is a 2D Graphic object containing a texture. In 2D mode, Unity uses sprites by default. When working in 3D mode, sprites will appear to be paper-thin, because they have no Z-width.
How to create Sprites in Unity?
You can create a Sprite in the scene by following these steps:
- Drag an image from an external folder into Unity. This will create a Texture Asset;
- You may need to convert this texture to a sprite in the inspector window;
- Drag this texture asset to the hierarchy window. This will create a new gameObject with the same name as your texture, with a” Sprite Renderer” component attached to it.
How to use the Sprite Creator in Unity?
The sprite creator tool helps you to create temporary placeholder 2D sprites with different shapes. You can later replace them with the graphics you want to use.
To use this tool, in the main menu select “Assets->Create->Sprites” and then select the placeholder sprite you want to create (square, circle, triangle, diamond, hexagon, or polygon).
Now drag your placeholder sprite to the Scene view or to the Hierarchy view to create a new sprite gameObject. To change your placeholder sprite, select the gameObject and then in the “Sprite” field of the “Sprite Renderer” Component in the Inspector, click on the small circle to the right of the input field to bring up the Sprite Selector where you can browse and select from a the available sprite assets in your project.
How to use the Sprite Editor in Unity?
The Sprite Editor is primarily used to extract elements (sprites) from a composite image, so that you will get one image asset with multiple sprites extracted from it.
You have to set the “Texture Type” on the image you have selected to “Sprite (2D and UI)” in order to see the “Sprite Editor” button in the inspector window.
Also, you need to set the “Sprite Mode” to “Multiple” in the Inspector in order to get sprite textures with multiple elements.
Select the image asset in the Project view and click on the “Sprite Editor” button in the Inspector and the Sprite Editor displays.
The “Slice” menu at the top left is the most important control in the Sprite Editor. With this menu you can automatically extract elements from the image asset. After any modification, you must click the “Apply” or “Revert” buttons to keep or discard any changes you have made.
We will not discuss all the options here, but if you choose the default slicing type which is “Automatic”, the editor will attempt to guess the boundaries of sprite elements by transparency.
You can also identify the elements manually by dragging rectangles in different parts of the image. You can drag the handles or the edges of the rectangles to resize them around a specific area. When you select one of these rectangles, a panel appears in the bottom right of the window. In this panel you can tweak the different values as you need.
In case of manual selection, the trim tool can help you resize a selected rectangle so that it fits tightly around the edge of the graphic based on transparency.
When you click on the little triangle on the right of the image asset that you have edited with the “Sprite editor”, you will see all the new extracted elements. Now you can drag this image from the Project window to the Hierarchy window (or to the Scene view) and choose which sprite to display in the “Select Sprite” window, or instead, drag an extracted element (sprite).