In the last article, we spent some time organizing our game by making use of empty game objects and the parent-child relationship. In this article, we’ll be making the leap from simple 3D objects to 2D sprites.
That actually might be a bit misleading. If you’re imagining that we’re going to convert the 3D objects to 2D objects, that is not what we are going to be doing in this article. I can tell you right now that such an effort would not be a good use of time. It involves stripping away the components that make a game object a 3D object, and then afterwards adding the components that make it into a 2D object.
All of that work is performed automatically when a new 3D or 2D object is created, so the best thing to do when converting from 3D to 2D is to just take the important parts you have custom built yourself and move them manually. More than likely, that just means the scripts you’ve created, so all you have to do is assign those scripts to the new objects.
But how do I get started with 2D?
And that’s the real question. When you start with 3D, everything is so (relatively) easy. You can create spheres, cubes, and more with just a click of the mouse. Then you can change their colors, apply physics, and numerous other functions with very little work. You can be ready to apply some custom code to a variety of 3D objects within minutes.
2D is a little different. You go to the 2D Object menu to create something, you select Sprite, and then what? Something was created, but you don’t see anything in the Scene window. What’s going on here?
With Sprites, Unity is expecting a bit more from us. We need our own assets, for starters.
Importing 2D Assets
The first thing we’ll need to do is get our sprites into the game. We can do this by right-clicking in our Project window and select the “Import New Asset…” command. Find your sprite (or sprites) and import them using the Unity Editor interface.
We are not quite done yet though. If you tried to use your newly imported asset to create a sprite in-game, it would not work. You need to configure settings directly on your assets in order to use them as sprites.
Click on your intended sprite, and then find the “Texture Type” setting in the Inspector window. To use it as a sprite, you will need to change this value to “Sprite (2D and UI)”. Also, lower in the window you will need to click the “Apply” button to confirm the change.
Applying the sprite to our object
Now that we have our sprite imported, applying it to our object is extremely simple. Find the “Sprite Renderer” component in your object, then just drag your sprite asset over to the “Sprite” field. And presto! We have a sprite in our game.
Alternatively, instead of manually creating a Sprite, you can just drag a sprite asset to the Hierarchy window and a Sprite object will be automatically generated with the asset in place.
Creating our Collider
There is still one thing we’re missing compared to our primitive 3D objects, and that is our Collider. Fortunately, that is another easy matter to fix.
In the Inspector window for our Sprite object, find the “Add Component” button at the bottom and then do a search for “2D Collider”. You will see a variety of options to choose from, so select the most appropriate option for your sprite. My own asteroid sprite is roughly circular, so I selected the Circle Collider 2D.
It’s very easy to shape the Circle Collider around my asteroid sprite using the Unity interface.
Of course, some of the other Collider types can be more precise, but this is a good starting point. We can also find other 2D components to apply to our sprite, such as a Rigidbody, but that’s a topic for another time.
Right now you might be wondering why we didn’t just start with 2D sprites instead of 3D primitives, and that is actually the subject of the next article. Until then, good luck and happy coding!