How to create a 2d game scene in Unity (Part 1)

Vinod Ravisankar, Andreas Loew
How to create a 2d game scene in Unity (Part 1)
Last update: More than 3 months ago

What you will learn

  1. Use TexturePacker to make sprite sheets
  2. Import sprite sheet to Unity with TexturePacker Importer.
  3. Make a simple animation in Unity
  4. Simple Player movement script in Unity

In this tutorial, we will use TexturePacker to make a sprite sheet and use it in a simple Unity game scene.

Ready? Let's get started.

Using TexturePacker to make sprite sheet

Download TexturePacker and install it on your computer

Follow the steps below to make your sprite sheet

  1. Open TexturePacker.
  2. Set the Data format in the right panel to Unity - Texture2D sprite sheet
  3. We are going to use a background sprite sheet, and a character sprite sheet for our game.
  4. Create separate projects in TexturePacker for background and character sprite sheets and name them Character-sprite-sheet and Background-sprite-sheet by clicking Save project in the toolbar. This will help you organise things.
  5. Drag and drop the folder containing your sprites onto the TexturePacker window. Add the character sprites to the character project and the background sprites to the background project.
  6. You should see a preview of your sprite sheet in the center view.

To create smaller sprite sheets, set Size constraint to AnySize - but this will give you warnings in Unity about mipmap creation. I recommend that you use **POT (Power of 2) **whenever possible.

Experiment with the packing by setting the Algorithm to Polygon. This algorithm is slower than MaxRects but allows you to pack more sprites on a single texture in many cases.

Click Publish sprite sheet to generate and save your sprite sheet as “Background-sprites” and “Character-sprites”. You will get two files when you publish a sprite sheet. One will be an image file (.png) and the other will be a data file with .tpsheet extension.

Sprite sheet preview

Importing the sprite sheet into Unity

Create a new 2D project in Unity. I am using Unity 2021.1.13f1 for this tutorial.

Unity new project creation window

You would have to split the sprites manually if you just copy and paste the sprite sheet into Unity. That’s why we’ve created a free Unity asset called TexturePacker Importer that automates the process for you.

In Unity 2020 or previous versions you can download the TexturePacker Importer from the Unity Asset store.** In case of Unity 2021 you need to use the Package Manager to Import the asset. **

Import the asset. You can exclude the example part from it if you want.

![TexturePacker Importer](TexturePacker Importer.png)

Once the import is complete you can copy the sprite sheet into the assets folder of Unity. Remember, you need to copy the .tpsheet file along with the .png file.

Unity Assets Folder

Creating a Sample Game scene in Unity

Click on the arrow on the Character-sprites PNG file and select all the sprites. Drag and drop the Character sprites to the hierarchy window or scene window in Unity’s editor. Unity will prompt you to make a new animation with these sprites. Give it a name and click save.

That’s it, you have created your character animation.

Walk animation

Click on the arrow on the Background-sprites PNG file Drag and drop your background sprites to create a simple environment. You can see mine in the image below.

Sceneview

The Character is much larger than the background. Use the scale option in the inspector window to reduce the size of the Character. You can also select the Character sprite and increase the Pixel Per Unit value to make your character fit into the scene.

Scaled character

You can attach the script below to the player gameobject for moving the player with arrow keys.

Adding player movement script
  1. Select the player in the hierarchy window.
  2. Go to the inspector window and click add component.
  3. Search for “new script”.
  4. Enter a name and save it.
  5. Open the script and paste the below code.

In the code, you take the input from the keyboard and add it to the player position.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playermove : MonoBehaviour
{
	Vector2 plamov;
	Quaternion rot;
	Animator anim;
    // Start is called before the first frame update
    void Start()
    {
		anim = GetComponent<Animator> ();  
		rot = transform.rotation;
    }

    // Update is called once per frame
    void Update()
    {
		plamov = transform.position;
		float posupdate= Input.GetAxis ("Horizontal") * Time.deltaTime*2f;
		//disable Animator when no key is pressed
		if (posupdate == 0) {
			anim.enabled = false;
		} else {
			anim.enabled = true;
		}
		//Rotate player when moving in the opposite direction
		if (posupdate < 0) {
			rot.y = 180;
			transform.rotation = rot;
		} else {
			rot.y = 0;	
			transform.rotation = rot;
		}
			
		plamov.x += posupdate;
		// Input.GetAxis takes input from the keyboard
		//Time.deltatime gives the time interval in seconds from the last frame to the current one. This makes the player movement device independent.
		transform.position = plamov;
        
    }
}

This is how your game should look like.

That's it for this tutorial. In the next part you will learn to use normals to create dynamic lighting in Unity.