From the course: Cert Prep: Unity Certified Associate Game Developer UI and 2D Games

Importing sprites and sprite sheets - Unity Tutorial

From the course: Cert Prep: Unity Certified Associate Game Developer UI and 2D Games

Start my 1-month free trial

Importing sprites and sprite sheets

- [Instructor] In this movie, we'll take a look at the raw ingredients of any 2D game or 2D project, and that's the sprite, To demonstrate this, I'm going to be importing two different types of sprites that you can find in the exercise files. These assets belong to the 2D game kit project we saw in the proceeding movie. In the exercise files, you can navigate to the assets to import folder, and inside there, there are two assets that I'm going to be importing. The first is the wall tiles sprite. This is a PNG image further we're going to use and repeat as a background texture. So I'm going to drag and drop that into the project panel, release my mouse, and you can already see it imported down here. In addition to the wall tile sprite, I'm also going to move to the idol folder where you're seeing a range of different textures. These are all individual frames of animation for the player character. I'm just going to select the very first one for the moment and drag and drop that into the project panel to import these two image files. You can see when I left click to select the player texture here in the preview panel here in the inspector, you can see how that image looks. It's a character on transparent backgrounds. The other texture is the wall tile that I'm going to be using to repeat across the background, to create a seamless wall. You'll notice that because I'm using the 2D framework here, all of these images and all of these textures are being configured as the texture type of sprite 2D UI. If you're using the 3D templates or one of the render pipelines, it's going to be imported as default. You want to make sure that these are selected as 2D and UI. And to begin to add these to the level, all I need to do is simply to drag and drop into the scene. And it's added into the scene as a completely separate and unique object, which I can select in the hierarchy. Notice that unlike UI elements, it is not controlled by a rect transform, but by a standard transform. So unlike the UI, the positioning and the sizes of these objects are not related to the screen directly, they're measured in terms of positions within the world. And in fact, I can switch out of the 2D preset and I can even see that my sprite has been added here in 3D is just located at zero on the Z axis. I'm going to go back into 2D mode here, and then I can take a look at my sprite. We have the ability to translate it using the standard translate tool, the W key. We can rotate sprites too if we want to, I'm going to undo that. I can also scale them and I can use the rect tool here to resize them if I want to. But in this case, this sprite is looking okay. Now, after adding this sprite to the scene, I also want to add the background, this wall tile here. So I'm going to drag and drop that into the scene. Now, the first thing that you should notice about this background here is that it appears behind the player character, and depending on how things have gone in your scene, it may appear behind and it may appear in front and the way you can control the ordering of sprites and the way they are set up inside the scene here, is to use the sorting and order in the lay here. So you'll see that every single sprite in the sprite renderer component, it has an additional settings rollout, and it has a sorting layer, and it has the order within that layer. Any two sprites that share the same sorting layer, and in this case the layer is default, I can click add sorting layer to add a new type of layer here. I'm going to stick with the default. Within the default layer, we have the wall tile, which you can see in the hierarchy that has the order of zero. And then if I select the player, and I'll rename this object here inside the hierarchy to player, you can see it also has the sorting order of zero. So purely by chance, we have this player character appearing in front of the wall. If I wanted to make it appear behind the wall, I need to give this a lower number in the order. If I always want it to appear in front, I need to make sure the number for its order here is higher than the wall tile number. And this will apply even if I moved to the scene for example, and I grab, let's say the player character, and I move it behind the wall it doesn't matter where on the Z axis it's moved. If the order here is higher, it will always appear on top. The other thing that you want to note here is that the wall tile background I've selected. If I switched to the game tab here, you can see the wall reaches to this point and then cuts off. And it zoomed in a little bit here. I can zoom out simply by moving to the free aspect, check the uncheck, the low aspect resolution here, and I can make sure that my scale here is set to one. I don't want this to be zoomed in. So here I'm viewing the character here with the wall reaching to this point and the wall terminates here. How can I have this wall repeat? I guess one way we could do that is by selecting the wall tile sprite, duplicating it like a regular 3D object, and then vertex snapping it to this position here. That is one way you can make a sprite tile, but it's not the only way. I'm going to select this duplicate, right-click and choose delete to remove it from the scene. You can select the original sprite texture down here inside the project panel, move to the object inspector, and under mesh type, where it says tight, you can change this to full wrecked, and then choose apply. When you do this, if you select the wall tile and move to the sprite renderer and change the drawer mode from simple to tiled, I can now select the sprite using the rect tool here, and begin to drag that off to the right hand side. And when I do that, you'll see that the sprite is not stretching, it is actually repeating and tiling. And the same happens here for the vertical axis. I can repeat and tile this for any length that I want, but I'm going to set it to about here and then check out how that looks inside the game tab. So now we have a player sprite character, standing in front of repeating wall. That's great. In the next movie, we're going to see how sprites can make our life easier when it comes to animation too.

Contents