The mini-lecture below provides a high level overview of what you'll learn in this section. Watch the video and take notes. Then, complete the lesson below to apply the concepts from the video.
Warning: It is not recommended to work along side the video as the content is not identical to the lesson.
Note: You can adjust the size of the preview by using the slider in the bottom right corner of the Project Explorer tab.
tile
model and drag it into the sceneBefore you add any more tiles to the scene, you should parent the tile model and create a Prefab to represent your tile. This will be useful if you decide to change the tile later (and you will).
tile
object in the Hierarchy and select Create Empty Parent this will create a new Game Object with the tile
as a child.Tile
Now that you have a Tile
Game Object, that will be the basis of each cell of your map, it is time to turn it into a reusable Prefab. To create a Prefab from an existing object, drag that object from the Hierarchy into your Project tab.
Tile
Game Object into your Assets
folder.You'll notice that the Game Object in the Hierarchy is now appears in blue and has a small arrow >
on the right side of the Hierarchy. This tells you that this Game Object is using a Prefab.
Note: You should use a Prefab any time you plan to use the same Game Object in multiple places (e.g., enemies, structures, UI elements). Most projects have many Prefabs and as such, it typically makes sense to create a folder to hold them.
For this game, you will need to create a grid of Tile
Game Objects that are aligned perfectly on the X, Y, and Z axis. This can be quite difficult and tedious to do even with just two Tiles! Try it.
You may have found it very difficult to perfectly align the two tiles, especially using the mouse! One option to align them perfectly is to use the inspector to se them to be exactly 1 unit apart (the tiles happen to be 1 Unity unit on the X and Z axis! LUCKY YOU!)
Using the above process, can you create a perfectly aligned 3x3 grid of Tiles?
When you are finished, your Hierarchy and Scene view should look similar to the image below:
After completing the previous challenge, you might be realizing how difficult, tedious, and painful it is going to be to create a large grid for your game in this manner. When you find yourself performing a repetitive task, it is often beneficial to seek if an automated solution exists and if it does not exists, create one.
In this case, there exists a partially automated solution (later you will create a fully automated solution).
You will often want to place Game Objects at an evenly spaced distance. This can be quickly don by using Unity's Linear Spacing Syntax. This allows you to specify a starting position and an ending position for a set of objects and Unity will automagically space the objects between those positions.
At this point, you should have 10 Tile objects spaced perfectly in a single row. Next, you need to create 10 rows that are evenly spaced.
Earlier in the lesson, you created a Tile Prefab. One of the major benefits of using a Prefab is the ability to change all Game Objects that use that Prefab throughout your project by modifying the original.
Double clicking on a Prefab asset in the Project explorer will open it in the Scene tab where it can be edited. You can exit the Prefab editor by clicking the <
near the top of the Hierarchy.
You may have noticed that the tile
model you previously added to the Tile
object is a prefab! Recall that you can easily identify prefab game objects by their blue color in the Inspector.
Some times, it is useful to unpack a prefab object to separate it from the parent Prefab. This can be done by right clicking on the Game Object in the Hierarchy and selecting Prefab > Unpack. When you do this, it disconnects it from the prefab such that it won't update if the original prefab changes. Because some of your Tiles will use different models, you will want disconnect the tile Prefab to allow the model to be changed.
tile
objecttile
object to be more generalized (I recommend model
)With the child object renamed, you can verify that all of your Tile Prefabs have changed exiting the Prefab Scene and opening your Tile objects.
You will often want to make changes to a single instance of a Prefab. For example, you will want to change some of your tiles to use a different model. This can be done by editing the Game Object directly in the Scene view's Hierarchy / Inspector.
In this case, you want to adjust some of your Tiles to use a different mesh. This can be set on the model
Game Object's Mesh Filter
. In the Inspector you can click the small circle next to the Mesh
field to see all mesh
assets in your project that can be set.
Mesh
of the model
Game Object to be tile-dirt
By clicking the >
button to the right of a Prefab in the Hierarchy, you can edit a Prefab in the Scene view. When you do this, all elements in the Scene view that are not apart of the selected object will be shown in grey scale indicating that you cannot interact with them in the Hierarchy.
>
button to enter the Prefab editor within the SceneWhile editing the Tile Prefab while in the Scene view, you can observe the changes live.
Mesh
property to be snow-tile
One thing to note is that the Prefab that you previously modified in the Hierarchy did not change. This is because the Mesh
property has been overridden. You can see all Overrides for a Game Object in the Inspector by selecting the Overrides drop down. Additionally, if you can Revert those changes to the Prefab's current setting or Apply those changes to the Prefab to propagate the change throughout your project.
Practice editing prefabs by setting the Tile Prefab to use a different mesh using each of the methods described above
Before continuing, update your Tile Prefab to use the tile
mesh. This particular Prefab will be used as an empty tile where the player can build a structure.
In the next section, you will create several more Prefabs that you will use to build a more detailed map