Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Setting up a 2D Unity project
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we're going to do the initial setup for our 2D game. All right, so if you are wanting to create a new 2D game, you'll want to go ahead and create a new project. So I'm going to go to File and New Project. And it's going to be exactly the same as it would be from before. You'll go ahead and you'll set up your project location, import any packages that you would like, and then you'll see a new added feature to this, where we're going to set up our defaults as 2D, OK? Now at this point you'll go ahead and hit Create. But seeing how I've already done that, you'll get something that looks like this. Now there's really not going to be anything that changes too much. The major thing that we're going to see is that the view is now defaulted at the 2D view, which gives us this orthographic view. You can turn that off by going back to your 3D view here, by just switching that toggle on and off, OK? And even if we're working in a 2D project, it's OK to work in this 3D view. There's nothing wrong with it. In fact, we'll be using this in a little while, so you'll see this come back here pretty soon. The next major thing that changes whenever you set up a 2D project is that your textures are going to come in as sprites, not necessarily as textures. So before Unity 4.3, your textures would come in as a texture type. So this meant that it was like diffuse textures or specular textures. Now they can come in as sprites, and it's easier for them to be a non-power of two. As most sprites are. We'll see that here in just a moment. If it any time that you decide to change your defaults from the 3D to 2D, you don't have to create a brand new project. In fact, what you can do is you can just simply go up to Edit, and then Project Settings, Editor, and then you could switch the mode right here. And you can switch that back and forth, either way. Now it's also worth mentioning that just because you are creating a 2D project does not mean that you cannot have 3D elements in your game. You can create 3D models, and you can add them in to your 2D game. Nothing wrong with that. All right, so now that we've kind of got past what is new as far as setting up our project, let's talk about 2D level design, OK? What is a process for doing that? Well, very roughly, what I like to do is I like to create just kind of a rough level layout. So let me go into Photoshop and kind of give you an idea of what I did. Looking at my layout level here, or layout layer, you can see that it's very, very basic. We've got these ground planes that the player is going to be able to walk across, they'll jump across this little gap, continue to run, jump across this one, all the way to the end. And it's going to be very much inspired from the original Super Mario Brothers. World 1.1. Very, very basic. And I think that that's fitting for this course here. So once I kind of have an idea of how the level is going to be laid out, you could start to sketch out ideas on how you want it to look aesthetically. As you can see here by the background, I've kind of gone with this sloppy, hand drawn type of look, to where we have just some basic colors. We have a two or three tone shading going on. Moving on into the foreground elements, you can see that we kind of have that same kind of style going on here. Now the way that I go about designing my levels, once I've got a rough idea, I start to separate my layers-- or my elements-- into different layers. So most likely I do my background elements, just like you saw here. And then I separate out my foreground elements. So these are basically going to be the elements that the player is going to interact with. Now, looking at this foreground element, or this layer, you'll notice that I have these clouds. Now these clouds is not specifically something that the player is going to be able to interact with, but these are going to be animating across the sky. So it's going to give us that parallaxing effect. And so I want to make sure that I've put those onto this layer, instead of the background layer, OK? Just to kind of show you the rest of the assets here that we're going to be using, I have the character involved as well. So this is our zombie character that we're going to be using for our game. And he's broken apart into several different elements. And we'll talk about that here in a little while, and we'll justify why we would want to break him up into different elements for this specific project. So I just wanted you to take a look at that. All right, now let's go ahead and jump back into Unity. Now at this point, what we want to do is, we want to import in all of our sprites that we're going to be using for our game. So going into my assets folder, I have sprites, and then inside of that I have my character and my environment folder. Let's go ahead and take a look at our environment folder, and you'll see that I have my background layer and then my foreground layer. Looking at my background layer there are a couple of things that we'll want to set up. First and foremost, we want to make sure that we are under sprite texture type, because we have those defaults set as 2D, this will always come in as a sprite. Next thing is the sprite mode. This is where you need to ask yourself, do I have multiple elements in the sprite sheet, or is it just a single image? In this case, the background is just a single image. Now the rest of this is OK for right now. We're going to go ahead and leave it just as it is. One thing that you'll want to make sure that you pay attention to, whenever you bring your sprites in, is that you're using the native size that you had intended. So for example, this background was drawn out at a 1280 by 720 canvas. And so I want to make sure that my max size is set to 2048. If I set it to 1024 and hit Apply, you'll notice that 1024 by 576, which is not the exact size that I had set it to. So you might get a little bit of depreciation in the quality of your image if you go too low. So I'm going to set that back to 2048, and hit Apply. Now that my background layer is set up and ready to go, let's go ahead and take a look at our foreground elements. So the foreground elements are separated out into what's called a texture sheet. A texture sheet holds multiple different images, or elements, known as sprites. And so looking at this texture sheet, we're set up as a sprite, which is perfect. But our sprite mode is set to single. Let's go ahead and click on that and set it to multiple. And what this does is it allows us to open up the sprite editor. So click on that, and you'll see that this window pops up. Notice that it's still a single image. We should be seeing some bounding boxes around these images that are separating it out from the main sprite sheet itself. So what I'm going to do is, I'm going to slice it apart, using the Slice tool here. So left click on that, and it's going to bring up the type of slice that we want to do, the minimum size of our slices, the pivot, and then the method. So we can Delete Existing, we could do a Smart Method, or a Save. I'm going to go ahead and just Delete Existing because I don't have any previous slices on this. So starting up at the top, we're set to automatic. But we get this warning here. It's saying automatic slicing of compressed textures gives imperfect results. And what that means is, it's going to do its best guess for slicing all of these apart. And so we may have to come in and do a little bit of editing. And we will for this particular texture. And I'll show you how to do that here in just a moment. So we have Automatic, but we also have Grid. So you might be asking, well what's the difference between the two? Well Automatic is going to be best for textures like this, where we have elements that are larger than others. They're not even in any way. But Grid will allow me to slice apart my sprite sheet in a very even way. So I can set up the cells, or the pixel size, to 64 by 64. And this is best if it was like a sprite sheet, a traditional sprite sheet, that you're used to seeing. So something like a character doing his idle animation, or his run animation. It's multiple frames in a single sprite sheet. If you want to take a look at the 2D racing game series that we've done in Unity, you can see in a perfect example of where this would be very helpful. Now we'll talk about how to use this later on with our coin, because we want to animate our coin spinning later on. And I've got a different sprite sheet for that all together. So let's go ahead and switch this back to Automatic, because this is going to work best for us in the situation we have at hand. So right now I'm just going to leave everything set as default, and I'm going to hit Slice. Notice these bounding boxes that pop up on the screen. Most of them look pretty good. We have some problems with this one right here, that's not going to be good for us. We also have this cloud, where it's trying to cut through half of this bush. Not really sure why that's happening, but we can always fix it. So to go about fixing something like this, let's go ahead and zoom in on our trap. Let's select the bounding box, just by left clicking right in the middle of it, and you'll see that it highlights as blue. I can take these corners, and I can scale those out and move them to where they completely encompass the sprite. Now I'm going to go ahead and move both edges out until it's completely encompassing that object. And then I'm going to hit Trim to trim out any excessive transparency. And this is perfectly set up here. Now the next thing that I want to do, and this is something that you should not overlook, is changing the name of the sprite. So for this, it's going to be set to trap, or spikes, whatever you want to call it. Let's go ahead and take a look at a rock, everything looks good there. Nothing out of the ordinary, it looks to be trimmed as it should be. The cloud, however got a little bit of an issue. So let's go ahead and drag it from right here, pull that over, and we're going to get pretty close to the cloud and then we're going to hit Trim. And you'll see that that trims that off, looks good. Now we have the bush, let's go ahead and bring that over. And let's make sure it comes up over the top there and below. Out to the side here, and then we'll hit Trim there, there we go. The coin looks pretty good. The ground plain looks good. We have the brain, and then we have the spring. So everything looks fine right now. Let's go ahead and rename everything. So I'm going to set this to ground, brain, spring. Let's try that one more time there. Rock, and then we have coin. Now if you happen to forget to rename these elements, it's OK. You can always come back to rename these. And I'll show you how to do that here in just a second. So let's go ahead and make sure that we've spelled that the way that we wanted to. And then what we'll do is we'll hit Apply. Now if you happen to forget to rename your elements, so if you come in and you expand out your foreground, you can see that we have all of these different sprites attached to that sprite sheet. If you happen to forget to rename those, all you have to do is go back to your Sprite Editor, and then left click on those, and then you can rename it. Make sure that you hit Apply after you finish. So now that we have learned how to set up our 2D projects, and we've also learned how to import our sprites and cut those apart, and we've even looked at a little bit about the Sprite Editor. In the next lesson, what I want to do is, I want to show you how to begin creating our level, and some things that we'll want to pay attention, whenever we start blocking that in. So we'll get into that next.
In this series of training we will discuss the major components of working with the 2D features in Unity. We'll start off by learning how to set up a 2D Unity project. Then we'll learn how to properly import and slice our sprites. After that we'll learn how to manipulate sprites by blocking in a simple platform level and also learn the importance of Sorting Layers.

Then we'll jump into the real fun by assembling, parenting and animating a character. By the end of this tutorial, you will have a better understanding of working with Unity's 2D features.
Introduction and project overview
1

Introduction and project overview

 
00:45
Setting up a 2D Unity project
2

Setting up a 2D Unity project

 
13:05
Manipulating sprites
3

Manipulating sprites

 
14:03
Setting up props
4

Setting up props

 
13:35
Slicing the character
5

Slicing the character

 
13:57
Parenting the character
6

Parenting the character

 
09:25
Creating the colliders
7

Creating the colliders

 
11:10
Creating the idle animation
8

Creating the idle animation

 
17:31
Blocking in the walk cycle
9

Blocking in the walk cycle

 
11:50
Finishing the walk cycle
10

Finishing the walk cycle

 
08:06
Setting up the animations
11

Setting up the animations

 
04:49
Scripting the movement
12

Scripting the movement

 
12:55
Using sprite sheets for animation
13

Using sprite sheets for animation

 
07:48