Mastering Unity 2D Game Development(Second Edition)
上QQ阅读APP看书,第一时间看更新

GameObjects and components

In Unity, every object in your scene will be a GameObject. A GameObject in itself isn't much other than a container for holding a list of Components. Components are the individual properties that make a GameObject unique. When a GameObject is selected, you can view each of its Components from its Inspector.

The most basic GameObject is an empty GameObject, which will contain no components other than a Transform. The Transform component is essential to each GameObject, as it allows it to have a physical location, rotation, and scale in the scene, as shown in the following screenshot:

You can create other types of GameObjects by selecting from a list of possible GameObject, starting with and empty one and adding individual components, or adding components to a provided GameObject.

Since we are working in 2D, the majority of our GameObjects will be a Sprite GameObject.

Sprite GameObjects

To create a Sprite GameObject, select Create | 2D Object | Sprite, as shown in the following screenshot:

The Sprite GameObject has two components, Transform and Sprite Renderer, as shown in the following screenshot:

Refer to the previous chapter to see more information concerning the Sprite Renderer.

Bringing our hero into the scene

Earlier, we imported our hero character's sprite sheet into our project and sliced up the sprites from that sheet to ensure they are ready to use. So, now let's bring her into the scene.

As with most things in Unity, there are two ways in which we can do this; first, we'll do this manually, and then we will use a shortcut route. The following steps describe the manual procedure:

  1. Create an empty Sprite in our game's Hierarchy for our hero by selecting Create | 2D Object | Sprite.
  2. Name it Player by changing the name in the inspector:

    The changes will be reflected in the Hierarchy, as shown the following screenshot:

  3. You will notice that there is no Sprite assigned to the Sprite Renderer. Select the small circle next to the Sprite slot:
  4. Select Protagonist_1 from the list that appears:

You should now have the same screen as shown in the following screenshot:

Bringing our hero into the scene
Unity's Editor window

Tip

A quicker method to create this Sprite is to drag and drop the Protagonist_1 image from the Project view into the scene and set the Transform Position to (0,0,0).

So, we have a character in a scene, but it's still not very interesting. A game is only truly a game if it has interaction. So let's allow our character to move about. But, before we can do that, we need to discuss some programming caveats.