Prefabs
A prefab is a group of objects saved as an asset (.plPrefab) so that you can place it many times. A lamp post, a door with its trigger, or an enemy with its weapon are typical prefabs. When you change the prefab, every place that uses it follows.
You open a prefab like any other document. The prefab editor looks like the scene editor: it has a scene tree, Properties, the 3D view and, beside Properties, the Prefab Settings tab (the prefab's exposed parameters) and the Variant tab (see Prefab Variants).
Creating a prefab
- In a scene, select the objects that make up the prefab.
- Right-click them in the scene tree or the viewport and choose Prefabs > Create Prefab....
- Pick where to save the
.plPrefabfile.
The selected objects are replaced by an instance of the new prefab.
Editor prefabs and engine prefabs
A prefab can be placed in two ways. Both reference the same prefab asset.
| Editor prefab instance | Engine prefab instance | |
|---|---|---|
| What the scene stores | A copy of the prefab's objects, remembered as coming from the prefab. | One object with a Prefab Reference component. The objects are created when the scene runs. |
| Per-instance changes | Any property of any object of the instance can be changed. The changes are kept when the prefab is updated. | Only the prefab's exposed parameters, under Parameters of the Prefab Reference component. |
| How to place it | Hold SHIFT while you drag the prefab from the asset browser. | Drag the prefab from the asset browser. |
| Cost | Every instance is saved in full in the scene. | Small: the scene stores the reference and the parameters. |
Use engine prefabs for anything placed many times. Use editor prefabs when individual instances need hand changes that you can't express as parameters.
The Prefabs menu
Right-click a prefab instance in the scene tree or the viewport and open Prefabs:
| Entry | What it does |
|---|---|
| Edit Prefab... | Opens the prefab in its own window. |
| Revert Prefab | Throws away the changes an editor prefab instance made, so it matches the prefab again. |
| Create Prefab... | Makes a new prefab from the selected objects. |
| Create Variant... | Makes a variant of the instance's prefab. |
| Switch Variant | Switches the selected instances to another prefab of the same family. |
| Save Overrides as Variant... | Writes the changes of an editor prefab instance into a new variant. |
| Open Variant Zoo | Opens a scene with one instance of every prefab of the family. |
| Unlink from Template | Turns the instance into ordinary objects with no connection to the prefab. |
| Convert to Editor Prefab | Turns an engine prefab instance into an editor prefab instance. |
| Convert to Engine Prefab | Turns an editor prefab instance into an engine prefab instance. |
In a prefab document the menu also has Remove Base Prefab, see Prefab Variants.
Prefabs at runtime
The Spawn component instantiates a prefab while the game runs, at intervals or when told to. Code can instantiate prefab resources directly as well. Both the Prefab Reference and the Spawn components can pick a random prefab of a family each time, see Randomize Variant on Spawn.