The Sprite Renderer is also required for the development of 2D projects. It defines the rendering of sprites. This can be used to create the illusion of depth in 2D games, for example.
What is a Sprite Renderer?
A sprite renderer is a very important component when it comes to developing 2D projects as it defines how a sprite is rendered. It is an important component of the tilemap system in order for the settings to work well.
Add the sprite renderer
If you are working in a 2D scene, the Sprite Renderer is very important because it lets you display images as a Sprite. This can also be used for 3D projects.
Add a new game component or a GameObject with Add Component. Go to the Components menu and choose Component -> Rendering -> Sprite Renderer
You can also add a GameObject that already has a Sprite Renderer:
GameObject–> 2D Object–> Sprite
You are adding a component that is already attached to the GameObject. This allows you to adjust settings to control how the GameObject is rendered.
Add two more GameObjects to your scene, and make sure they both have a sprite renderer.
From here you can see the two GameObjects, both of which have a sprite renderer. We use the Sprite Renderer to set how the Sprite and the two GameObjects react. Therefore, let's go through the options that can be set in the Sprite Renderer.
The settings for the Sprite Renderer
Select a Sprite GameObject that you see. You will be following Settings available to change the sprite renderer:
sprites: The sprite that is rendered: Sprite objects can be generated from textures by making Sprite settings during import or by creating them with the Sprite Editor Tool. You add the sprite to be rendered here.
Color: The colors of the rendered mesh for the sprite: This setting is useful for adjusting the color of the sprite.
Search the Sprite settingsn and add the sprite via drag and drop that you want to use for the object:
flip: This will mirror the sprite in the x or y plane and effectively mirror the sprite
Here is mirrored on the X-axis:
Here you are mirroring on the Y-axis:
Here you are mirroring on the X and Y axes at the same time:
Material: this is the material that will be used for the sprite: it is useful if you have special materials that you want the sprite to render.
Draw mode: this setting has several options with a drop-down menu that defines how a sprite will be scaled when you change its size.
Sorting Layer: This is the level that defines the priority of the sprites overlay when rendering. In short: is there a penguin in front of or behind the igloo? You define the levels for the sprite.
Order in layer: The overlay priority of the sprite within its own layer. Lower numbers are rendered first and subsequent numbers overlay those below. This is useful when you have a specific sprite that you want to hide behind other sprites on the same level. It has a similar function to the sorting layer.
Mask interaction: When sprite masking is used, this determines how the sprite behaves with the sprite mask. There are three possible options: None, Visible Inside Mask, Visible Outside Mask. This needs to be checked when using Sprite Masks.
Click on the Draw mode from the Sprite Renderer to get more settings.
Simple: This setting affects how the image is scaled in all directions when its size changes.
Sliced: Use this option if you're creating a 9-slicing sprite and want to enlarge those sections. In this mode, the corners remain the same size, while the top and bottom of the can expand horizontally. The sides of the sprite enlarge vertically and the center of the already changes horizontally and vertically to accommodate the sprite size.
Tiled: Use this option if you're using a 9-slicing sprite and want the sections to repeat. In this mode the sprite stays the same size and is not scaled. Instead, the top and bottom of the sprite repeat horizontally and the page repeats vertically. The center of the sprite repeats the tile shape of the tab, which corresponds to the size of the sprite.
Choose Tiled option from the list just to get more settings.
Size: Use this to adjust the horizontal and vertical change of the sprite. You need to make these settings to change the size of the sprite when using a 9-sliced sprite (9-part tiled sprite).
Tile mode: Use this setting to control how the sections repeat as the dimensions of the sprite change.
Advanced settings of the tile mode:
continuous: This setting is important when it comes to changing the size of the sprite, if it should change evenly.
Adaptive: this part is only repeated when the dimension of the sprite reaches the stretch value.
Stretch value: this value ranges between 0 and 1. One represents the image that is twice the size of the original. If the value is 1, the section will repeat when the image is twice the size of the original.
Sprite renderer materials
A sprite renderer uses textures to indicate sprite properties, but it also uses shaders and other materials. This means you can use the same material to change different sprites without worrying about which texture is associated with which material. The sprite is rendered on a mesh that uses position, color and UV at every point, but not a normal vector. If it can use normal vectors, you can do this with the vertex shader. There are two shaders that are used for sprites:
Sprites / Default: Ties is a simple alpha blended shader that doesn't interact with the light in the scene.
Sprites / Diffuse: This is a simple alpha blended surface shader that interacts with the light in the scene. The shader generates a forward-facing normal vector.
Flip sprites:
1. Take a second sprite and subordinate it to the first.
2. Select the parent sprite and assign it a -1 for the scaling in the X-direction
3. Take into account that it has side effects on all child sprites.
4. Reset the scaling to 1
5. Make sure you've selected the parent sprite and turn on the Flip-X option. These rotation settings are a lightweight alternative that does not affect any other components of the GameObject that are attached to the sprite. This makes it possible to rotate sprites individually, regardless of whether they are parents or children of other GameObjects.
Sort layers with sprites
Next, take a look at the Sorting Layers and see what benefits they can bring you. The soring layers help you organize how the sprites should be rendered. If you have an image that has a lower sorting layer, this is rendered first, such as a stone behind the player to simulate background objects. If the stone is assigned a higher sorting layer, it overlays the player avatar and becomes the foreground object.
1. Create 2 Sorting Layers and name the first one you want to create as background objects and the second as foreground.
2. Select the first sprite and add it as a background sorting layer
3. Take a second Sprite and add it to the Vodergrund Sorting Layer
4. Move both sprites into the same position. You will see: The foreground layer will overlay the background layer
5. Rotate the sprites layer and you will see the sprites reverse which sprite overlaps the other.
Unlike set in 3D games, there is no concept of depth in 2D games. It is important to have a way of defining priorities in the image so that the illusion of depth is created. For this reason, the sorting layers are very important for two sub-projects, as they allow sprites to be defined in groups with priorities. You can also set the priority of the overlap so that sprites exist on the same layer, for example, but can overlap each other. If the player's sprite is assigned a higher importance than, for example, a stone, the player is pushed to the foreground. This tells the renderer that the player sprite has priority.
It is important to understand how the sprite renderer works so that you can better control it. In your 2D projects, you can set how the sprites behave when they are on the screen. The more you use it, the easier it will be to make the settings you need.
Optimal sprite renderer settings for a 2D game
When configuring the sprite renderer for a 2D game, there are some basic settings you should consider to achieve the best possible rendering and performance. Here are the recommended settings:
- Sprite: Select the sprite that will represent your GameObject. Make sure it is well resolved and suitable for your game.
- Draw mode: In most cases, “Simple” is sufficient. If you want your sprite to span a larger area without becoming distorted, Sliced or Tiled might be more appropriate.
- Color: Defaults to white, meaning the sprite will be rendered in its original colors. You can use this option to create color variations without needing multiple sprites.
- Material: Use the default material unless you need special effects like glow or shadow.
- Sort layers & order in layers: These settings are essential for the correct arrangement of the sprites. Make sure your background objects are at a lower level and your foreground objects are at a higher level.
- Flip: Use this setting to mirror your sprite when necessary, such as when a character turns left or right.
- Mask interaction: This setting is only relevant if you work with sprite masks. Otherwise you can ignore them.
- Additional Settings:
- Pixel Perfect: Enable this option for sharper, pixel-perfect rendering, especially useful in pixel art games.
- Sprite pivot: Set the pivot point according to your requirements. For characters, this is often at the bottom of the sprite so that they stand correctly on the ground.
- Compression: Pay attention to the sprite compression setting in the import settings. Low compression can improve image quality, but results in larger file sizes.
- Filter mode: For pixel art games, “Point (no filter)” is often the best choice to preserve the clear pixel edges. For smoother sprites, Bilinear or Trilinear may be more appropriate.
- Anti-Aliasing: For games with lots of smooth curves and transitions, anti-aliasing can help soften edges.
These settings provide a good starting point. However, it's important to adapt them to your game's specific needs and visual design. Experimentation and testing are key to achieving the best possible representation and performance.
You can find more information in the Unity Manual
Originally posted on 2020-02-04 16:04:04.