Hands-On Unity 2020 Game Development
上QQ阅读APP看书,第一时间看更新

Integrating assets

We have just imported lots of files that can be used in several ways, so the idea of this section is to see how Unity integrates those assets with the GameObjects and components that need them.

In this section, we will cover the following concepts related to importing assets:

  • Integrating terrain textures
  • Integrating meshes
  • Integrating materials

Let's start using tileable textures to cover the terrain.

Integrating terrain textures

In order to apply textures to our terrain, do the following:

  1. Select the Terrain object.
  2. In the Inspector, click the brush icon of the Terrain component (second button).
  3. From the drop-down menu, select Paint Texture:

    Figure 5.14 – The Paint Texture option

  4. Click the Edit Terrain Layers… | Create Layer option.
  5. Look for the terrain texture you downloaded previously in the texture picker window that appears:

    Figure 5.15 – Texture picker

  6. You will see how the texture will be immediately applied to the whole terrain.
  7. Repeat steps 4 and 5 to add other textures. This time, you will see that that texture is not immediately applied.
  8. In the Terrain Layers section, select the new texture you have created to start painting with that. I used a mud texture in my case.
  9. Like when you edited the terrain, in the Brushes section, you can select and configure a brush to paint the terrain.
  10. In the Scene view, paint the areas you want to have that texture applied to.
  11. If your texture patterns are too obvious, select your texture in the Terrains Layer box and then, open the NewLayer N where N is a number that depends on the layers you have created.

    Important Note

    Each time you add a texture to the terrain, you will see that a new asset called "NewLayer N" is created in the Project view. It holds data of the terrain layer you have created, and you can use that one in other terrains if you need to. You can also rename that asset to give it a meaningful name. Also, you can reorganize those assets in their own folder.

  12. Open the section using the triangle to its left and increase the Size property in the Tiling Settings section until you find a suitable size where the pattern is not that obvious:

    Figure 5.16 – Painting texture options

  13. Repeat steps 4 to 12 until you have applied all the textures you wanted to add to your terrain. In my case, I applied the mud texture to the river basin and used a rock texture for the hills. For the texture of the rocks, I reduced the Opacity property of the brush to blend it better with the grass in the mountains. You can try to add a layer of snow at the top just for fun:

Figure 5.17 – Results of painting our terrain with three different textures

Of course, we can improve this a lot using lots of the advanced tools of the system, but let's keep things simple for now. Now, let's see how we can integrate the 3D models.

Integrating meshes

If you select one of the 3D assets we have configured previously and click the arrow to its right, one or more sub-assets will appear in the Project Window. This means that FBX is not a 3D model, but a container of assets that defines the 3D model:

Figure 5.18 – Mesh picker

Some of those sub-assets are meshes, which is a collection of triangles that defines the geometry of your model. You can find at least one of those inside the file, but you can also find several, and that can happen if your model is composed of lots of pieces. For example, a car can be a single rigid mesh, but that won't allow you to rotate its wheels or open its doors; it will be just a static car, and that can be enough if the car is just a prop in the scene, but if the player will be able to control it, you will probably need to modify it. The idea is that all the pieces of your car are different GameObjects parented with one another, in such a way that if you move one, all of them will move, but you can still rotate the pieces independently.

When you drag the 3D model file to the scene, Unity will automatically create all the objects for each piece and its proper parenting based on how the artist created them. You can select the object in the Hierarchy and explore all its children to see this:

Figure 5.19 – Sub-object selection

Also, you will find that each of those objects will have their own MeshFilter and MeshRenderer components, each one rendering just that piece of the car. Remember that the mesh filter is a component that has a reference to the mesh asset to render, so the Mesh Filter is the one using those mesh Sub-Assets we talked about previously:

Figure 5.20 – Mesh Filter current mesh selection

Now, if you drag the 3D model file into the scene, you will get a similar result as if the model were a Prefab and you were instancing it. But 3D model files are more limited than Prefabs, because you can't apply changes to the model, so after you've dragged the object onto the scene and edited it to have the behavior you want, I suggest that you create a Prefab to get all the benefits we discussed in Chapter 3, Working with Scenes and GameObjects, such as applying changes to all the instances of the Prefab and so on. Never create lots of instances of a model from its model file – always create them from the Prefab you created based on that file.

That's the basic usage of 3D meshes. Now, let's explore the texture integration process, which will make our 3D models have more detail.

Integrating textures

Maybe your model already has a texture applied, or maybe it has magenta applied to it. In the latter case, that means the asset wasn't prepared to work with the URP template you selected when creating the project. Some assets in the Asset Store are meant to be used in older versions of Unity:

Figure 5.21 – Mesh being rendered with erroneous or no material at all

One option to fix that is using Edit | Render Pipeline | Universal Render Pipeline | Upgrade Project Materials to UniversalRP Materials. This will try to upgrade all your materials to the current version of Unity:

Figure 5.22 – Upgrading materials to URP

The con of this method is that, sometimes, it won't upgrade the materials properly. Luckily, we can fix this by reapplying the textures of the objects in this new way. Even if your assets work just fine, I suggest that you reapply your textures anyway, just to learn more about the concept of materials.

A texture is applied directly to an object. That's because the texture is just one single configuration of all the ones that control the aspect of your model. In order to change the appearance of a model, you must create a material. A material is a separate asset that contains lots of settings about how Unity should render your object. You can apply that asset to several objects that share the same graphics settings, and if you change the settings of the material, it will affect all the objects that are using it. It works like a graphics profile.

In order to create a material to apply the textures of your object, you need to follow these steps:

  1. In the Project Window, click the plus (+) button at the top-left part of the window.
  2. Find the Material option in that menu and click it.
  3. Name your material. This is usually the name of the asset you are creating (for example, Car, Ship, Character, and so on).
  4. Drag this material asset you created to the model instance on your scene. At the moment, if you move the mouse with the dragged asset over the object, you will be able to see a preview of how it will look with that material. You can confirm this by releasing the mouse.
  5. Maybe your object has several parts. In that case, you will need to drag the material to each part of the object.

    Important Note

    Dragging the material will just change the materials property of the MeshRenderer component of the object you have dragged.

  6. Select the material and click the circle to the left of the Base Map property.
  7. In the Texture Selector, click on the texture of your model. It can be complicated to locate the texture. Usually, the name of the texture will match the model name. If not, you will need to try different textures until you see one that fits your object. Also, you may find several textures with the same name as your model. Just pick the one that seems to have the proper colors instead of the ones that look black and white or light blue; we will use those later:

Figure 5.23 – Base Map property of URP materials

With this, you have successfully applied the texture to the object through a material. For each object that uses the same texture, just drag the same material. Now that we have a basic understanding of how to apply the model textures, let's learn how to properly configure the import settings before spreading models all over the scene.