The Benefits of Prefabs - Unity Game Development Tutorial

In this Unity game development tutorial we'll be looking at how to make the most out of the prefab system.

The prefab system lets you store a GameObject and all of it's configuration as a reusable asset. If you want to have more than one instance of an object in a Scene or you want to use an object in multiple scenes, then you'll want to store that object as a prefab.

1. To get started let's create a new 3D project in Unity Hub.

Creating a new 3D project

2. Add a Plane to the scene by clicking the plus button on the Hierarchy panel and selecting 3D Object->Plane. We're going to use this plane to represent our floor.

Adding a plane to the scene

3. Create a new empty game object by clicking the plus button on the Hierarchy panel and selecting Create Empty. 

Creating an empty game object

4. Rename the new empty object to 'House' in the Inspector panel.


5. We're going to use this empty object to group lots of other objects together. Start by adding a cube by right clicking on the 'House' in the hierarchy and selecting 3D Object->Cube.

Adding a cube to the empty game object

6. Set the transform for the cube in the inspector panel to the values shown below

Setting the cube transformations

7. Now we'll create a prefab for our House so that it can be reused over and over again. To do this click and drag the House from the Hierarchy panel to the Project panel. You should now see a thumbnail representing the prefab.


8. If you now drag the prefab thumbnail from the Project panel onto the scene view it will create another instance of the prefab that is identical to the original except for its position.


9. Currently our House doesn't look much like a house. We'll sort that out shortly, but before we do we'll make a few changes to the appearance of the scene. First of all we'll set the lighting to auto generate. Select Window->Rendering->Lighting Settings from the main menu, and then tick the box labelled 'Auto Generate'

Setting lighting to auto generate

10. Next we'll add a material to the floor. To do this click on the plus button in the Project panel and select Material. Name the new material 'Floor'. Set the colour of the material to dark grey by clicking on the box next to Albedo in the Inspector panel and selecting the desired colour.

Setting the floor colour to grey

11. Assign the material by dragging it from the Project panel onto the Plane in the Scene view.

12. Now we can start improving our prefab. To do this click on the House in the Hierarchy and then select 'Open Prefab' in the Inspector panel.

Opening the prefab

13. From here we can modify our prefab. Let's start by adding a roof to our house. Click on the plus button on the Hierarchy panel and selecting 3D Object->Cube. Set the transform of the new cube to the values shown below.


14. The house will now have a roof. Click on the save button at the top right to save the changes to the prefab.

Saving the prefab

15. Return to the scene editor by clicking on the arrow next to the prefab name in the Hierarchy

Returning to the scene editor

When you return to the scene view you'll see that both instances of the house now have a roof. This is really powerful as it means you only need to make changes in one place to update many instances of an object.

Showing both instances of the house having a roof

16. It may be that we don't want all instances to appear or behave in exactly the same way. The prefab system supports this by allowing you to override values for individual instance. Let's add a chimney to one of the houses by right clicking on it in the Hierarchy panel and  panel and selecting 3D Object->Cube. Set the transform of the new cube to the values shown below.

Setting the transformations of the chimney

Now we have one house with a chimney and one without.

One house with a chimney and one without

17. If we now decided that we wanted all our houses to have chimneys we can apply the individual changes to the prefab. To do this click on the Overrides dropdown in the Inspector panel for the house with the chimney and select 'Apply All'.

Applying changes to the prefab

This applies the overrides to the prefab and to all instances.

Both houses with chimneys

18. Now let's add some colour to our houses. Add a new material in the Project panel and call it 'Walls'. Set the Albedo colour of the material to red.

Setting the Albedo to red

19. Add another new material. Call it 'Roof' and set the Albedo colour to brown.

Setting the Albedo to brown

20. Open the prefab editor for the House by selecting the prefab in the Project panel and selecting 'Open Prefab' in the Inspector panel. Now drag the Walls material onto the base of the house and the chimney, and the Roof material onto the roof.

House with materials assigned

21. The final thing we'll add to our house is a door. To do this click the plus button on the Hierarchy panel and select 3D Object->Quad. Set the transform of the quad to the values shown below.

Setting door transforms

22. Create a new blue material and drag it onto the door to assign it.

Setting Albedo to blue

23. Save the prefab and return to the scene to see that all the changes have been applied to both houses.

Changes have been applied to both houses

24. One other really powerful feature of prefabs is the concept of prefab variants. This allows you to override aspects of a prefab and store it as a prefab in its own right. Let's create a variant of our house that has a green door rather than a blue one. Create a new material and set the Albedo colour to green.

Setting the Albedo colour to green

25. Assign the new material to the door of one of the houses by dragging the material onto the door in the Scene view.

House with a green door

26. Now we have a house with a green door we can save it as a variant of the prefab. To do this drag the house from the Hierarchy to the Project panel. When prompted, select that you wish to create a 'Prefab Variant'

Saving the prefab variant

27. You'll now hove two prefabs. One with a blue door and one with a green one.

Two prefabs

28. Drag the two prefabs onto the Scene view a few times. Rotate and position them however you like and see how easy it is now to create a cluster of houses.

Cluster of houses

29. The other great thing about prefab variants is that they are still linked to the parent prefab and changes made there will apply to the variants. For example, if we open the original prefab for the house with the blue door and reposition the door quad as follows.

Repositioning the door quad

When we return to the Scene view, all the houses have the new door position regardless of whether they are blue or green!

Houses with new door position

That covers everything for this tutorial. We hope you found it useful. Please leave any questions or feedback in the comments below, and don't forget to subscribe to get notified when we publish our next post.

Thanks.

Comments

  1. Hey, I love reading this blog for the latest gaming news and updates!
    Publishing a Clicker Game

    ReplyDelete
  2. I recently went through a DiSC assessment, and it was eye-opening! The personalized insights into my strengths and potential areas for growth have been incredibly valuable. Grateful for this powerful tool! Quikdisc Card Game

    ReplyDelete

Post a Comment

Popular posts from this blog

Rotating a Character in the Direction of Movement - Unity Game Development Tutorial

Changing the Colour of a Material - Unity Game Development Tutorial

Creating Terrain from Heightmaps - Unity Game Development Tutorial