# Prefabs

Prefab is a resource used to store Entities, Components, and all attribute values ​​of entities and components. Prefab resources can be seen as a template for creating node instances in the scene.

If you want to reuse nodes in a scene, such as npc in a game, which may appear multiple times or across scenes, you can serialize the npc node into a Prefab resource at this time. In this way, only one Prefab resource needs to be modified each time, and the modification can be reflected in all the scenes where this Prefab is used, which is far more efficient and convenient than simple copy and paste.

The yellow planet in the picture above was created by prefab instantiation

In mini games, the prefab nesting mode is supported, that is, the nesting situation of other prefabs can be referenced in a prefab instance.

It also supports the mode of Prefab Variant (Prefab Variant). Prefab Variant allows users to modify a Prefab instance and serialize it into a new Prefab resource. Essentially, the root node of a Prefab Variant is another Prefab resource, so it can only be modified on the basis of the Prefab.

It also supports instantiating Prefab resources during game running, and mounting the Prefab instance to the scene.

# Create Prefabs

In WeChat mini-games, Prefab resources are regarded as a template. You can create a Prefab resource in the editor and save it in the Project window. For a Prefab resource, several Prefab instances can be instantiated. Prefab instances can be created during editing and saved to the scene, or they can be instantiated at runtime.

# Create Prefab Resources

There are two ways to create Prefab resources:

  1. As shown in the figure above, you can right-click to create a 2dPrefab or 3dPrefab on the right side of the Project window.
  2. As shown in the figure below, you can drag and drop the Hierarchy node into the Project folder to generate a Prefab resource with the selected node as the root node. The data of all child nodes and components of the node will be saved in the generated Prefab resources.

# Create a Prefab instance

There are two ways to create a Prefab instance in the editor:

  1. As shown in the figure above, right-click the node in the Hierarchy window, click "Add Prefab", select the Prefab resource in the pop-up box, and the instance of the Prefab resource will be added under the selected node.
  2. As shown in the figure below, you can drag the Prefab resource in the Project window to the node of the Hierarchy window to complete the creation of the Prefab instance.

For the root node of all Prefab instances, in the Hierarchy, there will be an arrow on the far right of the node. Clicking on this arrow will open a new Prefab instance in a new scene label.

# Create a Prefab instance when the game is running

Prefab can easily create complex node trees, and can easily use code to instantiate Prefab when the game is running.

import engine from "engine";
@engine.decorators.serialize("InstantiatePrefab")
export default class InstantiatePrefab extends engine.Script {
  public onAwake() {
    // Note: To load the Prefab resource at runtime, you need to set the resource as the entry resource in the editor
    engine.loader.load('NewFolder/Planet.prefab').promise.then((prefabAsset) => {
      // instantiate the prefab resource
      const prefabInstance = prefabAsset.instantiate()
      this.entity.transform.addChild(prefabInstance.transform)
    })

  }
}

# Create Prefab Variant Resources

The creation method is as shown in the figure above. Drag the Prefab root node in the Hierarchy to the Project, and there will be a pop-up window to choose whether to generate a Variant Prefab or Original Prefab. If you select Original Prefab, a normal Prefab will be generated, and if you select Variant Prefab, a Prefab variant resource will be generated, that is, the root node of the Prefab is the root node of another Prefab instance (as shown in the figure below).

# Edit Prefab

# By opening Prefab resource editing

In the Project, double-click the Planet.prefab resource to open the Prefab, as shown in the figure above. Nodes and components can be added or deleted in Hierarchy, and the Prefab resource will be updated after saving.

If "Add Prefab" is added to the Prefab, as shown in the figure below, a Prefab instance with the root node of explosionPartical is added to form a prefab nested mode.

# Edit by Prefab instance

As shown in the figure above, there is an instance of Planet Prefab in the mygame scene, and a NewEntity node is added to this instance. In the editor, the editing of Prefab instance nodes and components is supported:

-Add nodes -Modify node attributes -Add components -Modify components -Remove components

There are three types of operations on Prefab instances:

-Prefab Unpack: Degenerate the Prefab instance into a node of the scene itself. -Prefab Revert: Revert all modifications on the Prefab instance. -Prefab Apply: Save all the modifications on the Prefab instance to the Prefab resource.

点击咨询小助手