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
Name | Description | Example |
---|---|---|
Prefab Asset | The asset of prefab in Assets panel, which is the serialize file of Prefab. | |
Prefab Instance | When 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 green | |
Prefab Editing Mode | Double-click the Prefab Asset to enter the prefab editing mode. At this time, all non-nested prefab nodes are displayed in dark green | |
Nested Prefab Instance | When a child node in a Prefab Asset is an instance of another Prefab Asset, we call this child prefab a nested prefab instance |
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.
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.
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.
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:
Icon | Description |
---|---|
Revert 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 . | |
Locating assets. It is convenient to quickly locate Prefab Asset in the Assets panel. | |
Restore 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. | |
Update 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.
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