官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 绥宁县| 玛曲县| 图木舒克市| 宿迁市| 城市| 平远县| 双柏县| 太湖县| 曲水县| 四会市| 阜宁县| 增城市| 永善县| 庆云县| 大渡口区| 乐都县| 和田市| 沂南县| 广宁县| 金坛市| 通州市| 凉城县| 阿拉善左旗| 龙岩市| 双鸭山市| 柏乡县| 哈巴河县| 孟村| 天气| 旬邑县| 筠连县| 安徽省| 梅州市| 酉阳| 浏阳市| 乌兰县| 厦门市| 河西区| 兴业县| 霍州市| 资中县|