In my last article, I made a few 2D assets for my game. I can now import them into Unity and replace my 3D primitive assets.
While there are a few ways to import assets into Unity, this is how I like to go about it. I first make a sprites folder, with sub-folders for enemies, player, and weapons, in my project window. I then drop the PNG assets into their respective folders in my Unity project folder, on my hard drive.
When I go back into Unity, those files will now show in the project window, almost ready to import.
With the PNG file selected in the project window, the inspector will show quite a few import options. My PNG assets have a transparent background, so in order to view them properly, the Texture Type needs to be set to Sprite (2D and UI). Hit apply to save the changes.
As seen in the following GIF, bringing assets into the game is as easy as dragging and dropping them from the Project window into the Hierarchy.
My fierce baby Narwhal is now visible in the scene and game views!
After also bringing my weapon and enemy into the hierarchy, I need to take note of some 3D to 2D conversion changes. For starters, the 3D based mesh renderer has been replaced with a sprite renderer.
For the most part, not too much will be changing. There are however, a few items that will need to be addressed when transitioning from 3D assets to 2D sprites. I need to change the Box Colliders on my player, enemy and weapon, to be a Box Collider2D. In this case, I want Unity to use the outline shape of my object as it’s collider, so I will be using a Polygon Collider 2D. I also set Is Trigger to be true, because I want pass-through collisions.
I also need to change the Rigidbody on my enemy to a Rigidbody2D. While the linear drag is similar to the 3D based Rigidbody, the Use Gravity option has changed to Gravity Scale. A scale of zero will keep the game objects from falling off the bottom of the screen with real gravity.
The 2D sprites now need their respective scripts attached. This can be done through the Add Component button in the inspector, or by simply dragging the script from the scripts folder and dropping it directly on a game object.
The data in the inspector that was serialized probably isn’t up to date with what I have currently on my 3D player. That can be fixed by right clicking on the player script component in the inspector, and selecting copy component. Then I can go to the new 2D player sprite script component, right click and paste component values to update it.
Once my components are all reassigned and up to date, I can delete my old 3D PreFabs and drag my new 2D weapon and enemy game objects into the PreFabs folder.
My enemy will need to change the GetComponent for itself, being it’s not looking for 3D components any more. The variables as well as well as void start needs to be updated to reflect 2D Components.
I also need to change OnTriggerEnter, to OnTriggerEnter2D to reflect the 2D collider components I just attached. In the first brackets the “collider” also needs to change to (collider2D other).
My useGravity needs to change to gravity scale and I give it a value of one, being this is causing my little fish to drop off the screen after being hit by a Narwhal tusk.
After saving, I can play test my game with my new assets!