Create a component for the spawner example
This task shows you how to create an entity component system (ECS) component to store data for the spawner example. Subsequent tasks in this workflow use the data in this component to determine which entity the spawner should instantiate, how often to instantiate the entity, and where to instantiate the entity at.
Before you create a component, you should think about the kind of data that the component will store and in what context you will use it. You can then decide which component type to use to implement the component. The component for the spawner example will store:
- A Prefab to instantiate.
- A position to instantiate the Prefab at.
- The rate at which to instantiate the Prefab.
- The last time that the spawner instantiated the Prefab.
The most appropriate component type to store this kind of data is an unmanaged component.
ECS workflow overview
This task is the second 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 component
- Create a new C# script called
Spawner
. - Replace the contents of the file with the following code example.
using Unity.Entities;
using Unity.Mathematics;
public struct Spawner : IComponentData
{
public Entity Prefab;
public float3 SpawnPosition;
public float NextSpawnTime;
public float SpawnRate;
}
Next steps
To continue to create and optimize the spawner system, follow the next task in this workflow: