Skip to content

Create sprite entity

TonyMax edited this page Feb 8, 2023 · 6 revisions

Lastly we just need to create proper entity. It is simple as this:

// initialize sprite entity with all needed components for rendering
entityManager.AddSpriteRenderComponents(spriteEntity, renderID);
// WorldPosition2D and SpriteColor are example client's components
entityManager.AddComponentData(new WorldPosition2D());          
entityManager.AddComponentData(new SpriteColor(Color.White));

Or do it from your baker class

private class Baker : Baker<SpriteAuthoring>
{
    public override void Bake(SpriteAuthoring authoring)
    {
        AddComponent(new WorldPosition2D { Value = new float2(authoring.transform.position.x, authoring.transform.position.y) });
        AddComponent(new SpriteColor { Value = Color.White });
        this.AddSpriteRenderComponents(authoring.RenderID); // Render ID is client defined unique per-render archetype int. You can define it manually or for example use Material's instance ID or whatever else.
    }
}

Note: This methods required because entity should have some internal components like it described here.