Create the spawner entity for the spawner example
This task demonstrates how the baking process creates a primary entity for each authoring GameObject. It then shows you how to create an authoring GameObject and use a baker to attach an entity component system (ECS) component to the resulting primary entity. This creates an instance of the ECS component that systems can query, transform, and write to.
To create an entity and attach ECS components to it, you need to create the following:
- An authoring component which is a MonoBehaviour component that holds values that you can pass from the Editor to the ECS component.
- A baker to attach the ECS component to the entity, and populate the ECS component with values from the authoring component.
ECS workflow overview
This task is the third task in a series of five tasks that show you how to create and optimize behavior in an ECS system. At the end of the tasks, you will have a spawner system that reads and writes component data, and instantiates entities. This workflow contains the following tasks:
- Create the subscene for the spawner example
- Create a component for the spawner example
- Create the spawner entity for the spawner example
- Create the system for the spawner example
- Optimize the system for the spawner example
Each task is a prerequisite for the subsequent tasks.
Create the spawner entity
- Create a new C# script called
SpawnerAuthoring
and replace the contents of the file with the below code example. This code example contains both the authoring component and the baker. - Create an empty GameObject called Spawner in your subscene and attach the SpawnerAuthoring component to it.
- Create or source a Prefab to spawn.
- Select the Spawner GameObject and, in the Inspector, assign the Prefab to the Prefab property and set Spawn Rate to 2.
- Open the Entities Hierarchy window and set the data mode to either runtime or mixed. These data modes both display the entities that the baking system generates from the authoring GameObjects.
- In the Entities Hierarchy window, select the Spawner entity. The Entities Hierarchy window displays both GameObjects and entities. To distinguish between the two, entities are indicated by a hexagon icon ().
- In the Inspector for the Spawner entity, open the Entity Baking Preview. This displays the attached Spawner component and the component values that the baker set.
using UnityEngine;
using Unity.Entities;
class SpawnerAuthoring : MonoBehaviour
{
public GameObject Prefab;
public float SpawnRate;
}
class SpawnerBaker : Baker<SpawnerAuthoring>
{
public override void Bake(SpawnerAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new Spawner
{
// By default, each authoring GameObject turns into an Entity.
// Given a GameObject (or authoring component), GetEntity looks up the resulting Entity.
Prefab = GetEntity(authoring.Prefab, TransformUsageFlags.Dynamic),
SpawnPosition = authoring.transform.position,
NextSpawnTime = 0.0f,
SpawnRate = authoring.SpawnRate
});
}
}
Next steps
To continue to create and optimize the spawner system, follow the next task in this workflow: