November 16, 2021

Prefabs

 
A prefab is an asset that contains a serialized entity and can be instanced in your scene. It’s useful when creating many instances of the same entity type like columns, chairs, UI components, etc. We can just duplicate the entities, but using prefabs is more efficient because making some changes in the prefab will automatically propagate them to all its instances in the scenes.

Evergine is adding an early version of our new Prefab system, with a core of new features while we’re working on adding new interesting ones in the short future. In this first version, we support the prefab creation and instancing, and we plan to add a full feature-complete editor in our next version.
 

Creating prefabs in Evergine Studio

Right now we can create a prefab by selecting an entity, right-clicking on it, and selecting the “Create prefab” item in the context menu.

This will create a Prefab asset named like the entity in the current asset folder.


 

Instantiating prefabs in Evergine Studio

To instantiate a prefab in your scene you just need to drag a prefab asset into the scene viewer and a new Prefab instance will be created.

Once instantiated, the Prefab Instance in your scene can be modified and displaced, and the scene will keep track of those changes. In this example, we created a simple mini-forest.
 

Instantiating prefabs by code

Instantiating a prefab in code is also really simple. We just need to call the Instantiate method of the Prefab object.

var treePrefab = this.assets.Load<Prefab>(EvergineContent.Prefabs.tree);
var treeInstance = treePrefab.Instantiate();

this.Managers.EntityManager.Add(treeInstance);

Antonio avila
Author
Antonio Ávila
Plain Concepts Research
Categories