Skip to content

Prefab

Prefab is used to store some scene objects that can be reused, it can contain nodes, components, and data in components. The instances generated by the prefab asset can not only inherit the data of the template, but also have it's own customized data modification.

Basic Concepts

NameDescriptionExample
Prefab AssetThe asset of prefab in Assets panel, which is the serialize file of Prefab.prefab asset
Prefab InstanceWhen the Prefab Asset is drag to the Hierarchy panel, it will generate a Prefab Instance. Its root node is currently marked in dark green, and its child nodes are bright greenprefab instance
Prefab Editing ModeDouble-click the Prefab Asset to enter the prefab editing mode. At this time, all non-nested prefab nodes are displayed in bright greenprefab edit mode
Nested Prefab InstanceWhen a child node in a Prefab Asset is an instance of another Prefab Asset, we call this child prefab a nested prefab instancenested-prefab

Creating a Prefab

There are two ways to create a prefab:

  1. After editing the Node in the Scene, drag the Nodes directly from the Hierarchy panel to the Assets panel to complete the creation of the Prefab.

  2. Click the + button at the top left of Assets, or click the blank space of the panel, and then select Node Prefab. (New in v3.1.1)

    create

After the creation is complete, the original Node will automatically become a Prefab Instance, its root node is currently marked in bright green, and its child nodes are dark green.

create-prefab

Using Prefabs

Drag a Prefab Asset from the Assets panel to the Hierarchy panel or Scene panel to create a Prefab Node in the Scene.

In the Scene, the Prefab Instance objects data source comes from the deserialization of the Prefab Asset , so its data is synchronized with the Prefab Asset by default. If the attributes in the prefab instance are modified, the modified data will be stored In the Prefab Instance, it will not affect the Prefab Asset and the data of other Prefab Instance generated by it.

use-prefab

Entering prefab editing mode

Double-click the Prefab Asset in the Assets panel to switch from Scene editing mode to Prefab editing mode. It is possible to edit the Prefab Assets in the Editor. After editing, click Save button in the Scene panel to save the edited Prefab Assets. Next, click the Close button to return to the Scene editing mode.

Note: please avoid multiple people modifying the same prefab asset at the same time, as this may lead to conflicts that cannot be resolved by git merging.

edit mode

Status of prefab nodes

Prefab Nodes in the Inspector panel render green to indicate normal association with assets and render red to indicate that the associated assets no longer exist.

Editing Prefab Nodes in a Scene

General Operations

In the Hierarchy panel, select the Prefab Node, and notice there are several buttons that can be clicked at the top of the Inspector panel:

edit prefab

Button IconDescription
unlink prefabRevert to normal node. Prefab Instance can become ordinary Nodes, that is, completely separated from the relationship between assets. This function is also available in the top-level menu Node. Support batch disassociation in v3.4.
locate prefabLocating assets. It is convenient to quickly locate Prefab Asset in the Assets panel.
revert prefabRestore from asset. Restore the data of the current Prefab Instance to the data in the Prefab Asset, but the name, location, and rotation will not be restored.
apply prefabUpdate to asset. Update all data of the current Prefab Instance to the associated Prefab Asset.

Add New Node

A new node added under the Prefab Instance will have a + sign in the lower right corner of the node name, and its data is stored under the Prefab Instance, so it will not affect the data of the associated Prefab Asset.

prefab mounted

Add New Component

A new component added under the Prefab Instance will have a + sign at the end of the node name, and its data is stored under the Prefab Instance, it will not affect the data of the associated Prefab Asset.

instance-add-component

Remove Component

A new component added under the Prefab Instance will have a deleted data item show in the Inspector, its data is stored under the Prefab Instance, it will not affect the data of the associated Prefab Asset.

instance-remove-component

It will show two buttons in the end of deleted data item:

Button IconDescription
revert-remove-componentRevert the removed component.
apply-remove-componentApply the operation of removing the component to the associated Prefab Asset.

Some Current Restrictions

  • It is not allowed to delete the node created from the Prefab Asset in the Prefab Instance.
  • It is not allowed to change the hierarchical relationship of nodes created from the Prefab Asset in the Prefab Instance.
  • Do not allow prefabs to nest themselves.