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

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.

主站蜘蛛池模板: 禹城市| 长治市| 天水市| 兴安县| 措美县| 临潭县| 泾川县| 太湖县| 诏安县| 吉隆县| 嘉峪关市| 昭苏县| 宁陕县| 和平区| 驻马店市| 临澧县| 岳西县| 新竹县| 德州市| 房产| 巴彦县| 尖扎县| 溆浦县| 延边| 衡阳市| 涿鹿县| 南昌市| 石阡县| 阿城市| 景泰县| 松原市| 泗水县| 衡山县| 隆安县| 都江堰市| 望都县| 开鲁县| 上思县| 成武县| 永新县| 海伦市|