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 - 图1
Prefab InstanceWhen the Prefab Asset is drag to the Hierarchy panel, it will generate a Prefab Instance.Its root node is currently marked in bright green, and its child nodes are dark greenPrefab - 图2
Prefab Editing ModeDouble-click the Prefab Asset to enter the prefab editing mode. At this time, all non-nested prefab nodes are displayed in dark greenPrefab - 图3
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 instancePrefab - 图4

Creating a Prefab

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. 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.

Prefab - 图5

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.

Prefab - 图6

Entering prefab editing mode

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

Prefab - 图7

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:

Prefab - 图8

IconDescription
Prefab - 图9Revert 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.
Prefab - 图10Locating assets.Its convenient to quickly locate Prefab Asset in the Assets panel.
Prefab - 图11Restore 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.
Prefab - 图12Update 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 - 图13

Some Current Restrictions

  • Add/remove components in the Prefab Instance
  • Delete the node created from the Prefab Asset in the Prefab Instance
  • Change the hierarchical relationship of nodes created from the Prefab Asset in the Prefab Instance
  • Do not allow prefabs to nest themselves