Creating components and entities from scratch

Hi.

I’m looking for example of how to create an entity and its components from scratch. I’ve seen endless of videos and other examples, but not a single one where the data is created from scratch, and I really don’t want to create GameObjects and/or Prefabs to convert into Entities as a middle step.

so I guess something like this would work? When not converting from a GameObject, what components do I need to get the entity drawn on screen? In examples I see mosty just having components for position and rotation.

entityManager = World.Active.GetOrCreateManager<EntityManager>();
Entity myEntity = entityManager.CreateEntity();
entityManager.AddComponent(myEntity, myTransform);

To draw something on screen you need at least RenderMesh and LocalToWorld components.

2 Likes

You can create a EntityArcheType as a way of writing out all the Components that you want to have on your entity (a kind of prefab in code).

To get the object to render, you need a RenderMesh-Component, and I believe a LocalToWorld-Component (for positioning).
The RenderMesh-Component would need a mesh and a material to render, but you can load those using Resources.Load()

I managed to get the objects on screen using RenderMesh and LocalToWorld as suggested above, but also had to add Translation,

        entityManager.AddComponent(myEntity, typeof(RenderMesh));
        entityManager.AddComponent(myEntity, typeof(LocalToWorld));
        entityManager.AddComponent(myEntity, typeof(Translation));

Thanks for putting me on the right track, @GilCat and @FrankvHoof .

Translation is used as a ‘position’, but will be added to the Entities LocalToWord-Matrix by one of Unity’s Systems (I believe it’s the EndFrameTransform-System.)

You don’t really need it, because you can set the position directly to the matrix if you want to, but you can use the Translation-Component (and the Rotation- and UniformScale- ones) as well.
Just make sure you actually have a LocalToWorld that the RenderSystem can eventually read from.

1 Like

You can read more about the Transform System here.

1 Like