Prefab Asset

From Pummel Party Mod Documentation
Jump to navigation Jump to search

A prefab is a collection of gameobjects formed into a sub-hierarchy that can be reused. Changing a prefab will cause all instances of that prefab to be updated.

Prefabs can be viewed in the Asset Browser in the Custom > Prefabs folder.

Creating a prefab

From the hierarchy

You can turn a group of gameobjects in the hierarchy into a prefab. This group must have a single parent gameobject that you can select; you can't multi-select gameobjects to turn into a prefab. To create the prefab, select the parent gameobject and drag it from the Hierarchy Window into the Asset Browser. This will create a prefab containing the dragged gameobject sub-hierarchy.

Directly from the Asset Browser

In the asset browser right click and choose Create > Prefab. This will create an empty prefab that you can populate with gameobjects and components.

Editing a prefab

To edit a prefab, find it in the hierarchy and double-click it. You can also select it in the hierarchy or scene view so that it is visible in the Inspector Window. Here you can click the Edit Prefab button on the Prefab Instance Component.

Alternatively, you can locate it in the Asset Browser and double-click it.

Instantiating prefabs

Prefabs can be instantiated in the scene by creating a Prefab Instance and setting the Prefab field to the prefab you'd like to use. This will place that prefab into the scene.

Use cases

Re-using groups of gameobjects

This is the primary usage of prefabs. If you build a house of many gameobjects (walls, floors, doors, windows, decorations etc) and turn it into a prefab, you can then create many instances of that prefab throughout the scene. If you then realize you forgot to add a back door to the house, you can update the prefab and all houses you've placed will update. If you hadn't used a prefab, then you'd need to manually add a door to every house you'd placed.

Usage by other components

Some components such as the Item Component and Spawner Component require a prefab to be passed to them. The item uses this prefab as the visual object that the player can hold when using the item, and the spawner needs a prefab of objects for it to spawn.