Prefabs

When the scene begins to fill up with objects, some of these objects might become complex. By complex, I mean with a lot of components and children. If we need to use many of them in the game and maybe change all of them at once, Unity offers the possibility to create a Prefab.

As the name suggests, it is an object already assembled with all the necessary components and ready to be placed in the scene. The advantage is the possibility to reuse it often and to change all its instances quickly.

Note

If an object in the scene is a Prefab, its name in the Hierarchy panel is blue. If the name is red, instead, this means that there are some references missing.

To keep our project organized, let's create a folder named Prefabs, if we haven't done so yet. Inside the folder, right-click and then select Create/Prefab. You can name it as you want, but for this book, let's stick with Pink_Sprinkle_Projectile_Prefab.

We already have the sprinkle in the scene, so from the Hierarchy panel, drag it into the Pink_Sprinkle_Projectile_Prefab.

Now, we can also erase the previous sprinkle from the scene, as we don't need it anymore. For testing purposes, you can try to add as many sprinkles as you want in the scene by dragging the Prefab into the Scene view. Of course, remember to erase them before continuing with the rest of this chapter.

When we select an object that is an instance of a Prefab, three additional buttons appear in the Inspector, as shown in the following screenshot:

Prefabs

These are their functions:

  • Select: This is a shortcut to quickly select in the Project panel the Prefab of the object.
  • Revert: If we did some changes in the instance of the Prefab (the current selected object), these do not affect the Prefab. By clicking on this button, we revert all the changes to the original Prefab.
  • Apply: If, on the other hand, you are satisfied with the changes you made to this instance of the Prefab, by clicking on this button, you can apply these changes to the Prefab. As a result, you may modify all of the other instances in the scene. So be careful when you use this function.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.54.7