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.
There are two ways to create 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.
-
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)
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.
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.
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.
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.
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:
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.
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.
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.
It will show two buttons in the end of deleted data item:
Button Icon | Description |
---|---|
Revert the removed component. | |
Apply the operation of removing the component to the associated Prefab Asset. |
- 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.