Create Prefabs to manage a large number of objects

Page update date :
Page creation date :

Verification environment

Windows
  • Windows 11
Unity Editor
  • 2021.3.3f1
Input System Package
  • 1.3.0

Prerequisites for this tip

The following settings have been made in advance as a premise for the description of this tip.

About Prefabs

To put it simply, Prefabs are a function that allows you to centrally manage objects as a replication source.

For example, let's say you create an enemy character with 100 HP and duplicate and place it to place 100. However, if you want to get 50 HP for game balance, you'll need to reset the HP of 100 to 50. If you only have one type, there are still more settings in the game, so it's hard to do this every time and you can make mistakes.

If you use Prefabs, let's say you want a Prefab enemy character to have 100 HP and place 100 enemy characters "based on a Prefab". The settings of the duplicated enemy characters are held by "Prefab", so by setting the HP of the prefab to 50, the HP of all the duplicated enemy characters will be 50.

By the way, HP is used as an example, but there is no HP in Unity's object settings, so please consider it as an example of sharing parameters.

Creating a Prefab

First, create a new project.

Next, create the object that you want to clone. This is no different from creating a single object normally. Here we will add an image file to the project and place it as a sprite.

]

Now create a prefab. When you place an object in the view, it is added to the hierarchy, so drop it into the project.

You will see that it will be added to the project in a slightly different form than the original image file. When you select the file path below has the extension ". prefab" and you can see that a blue cube icon is displayed.

Actually, this is a prefab, and you can create a prefab with just this operation.

By the way, if you check the hierarchy, you can see that the icon of the object you moved earlier is blue. This indicates that this is an object created from a Prefab.

Try dropping the Prefab you created in your project into the view. I think an image (sprite) has been added to the view, and the hierarchy icon is also blue.

Since it is an object created based on a prefab, let's add more and more.

Here, for example, let's change it assuming that the size of the image was small and must be doubled. First, select the prefab file you have in your project. This will bring up the prefab settings selected by the inspector.

Let's set the XYZ of the scale to 2 for each.

Then, you can see that the scale of "objects generated from prefabs" changes all at once.

Prefabs can share settings like this, so if you have a large number of objects to be placed, it is a good idea to use prefabs.