Creating GameObjectEntity from Archetype?

When I want pure Entity, I can create it from Archetype like this:

PlayerArchetype = entityManager.CreateArchetype(typeof(Position), typeof(Heading), typeof(PlayerInput));
...
entityManager.CreateEntity(Archetypes.PlayerArchetype);

Now I wonder, is there a similar way to create GameObjectEntity from Archetype?
Currently I’m doing this, feels wrong:

var cameraEntity = Camera.AddComponent<GameObjectEntity>().Entity;
entityManager.AddComponent(cameraEntity, typeof(Position));
entityManager.AddComponent(cameraEntity, typeof(Heading));

I’ve tried to do this:

cameraEntity.Entity = entityManager.CreateEntity(Archetypes.CameraArchetype);

But GameObjectEntity’s Entity property is readonly.

I realized GameObjectEntity doesn’t automatically synchronize component data and gameobject’s transform, so I actually don’t need Position/Rotation/Heading in my camera entity for now, but the question is still important for more general case.

You want an equivalent result that GameObjectEntity produces? In that case I believe you are only missing the ability to inject that entity as GameObjectArray after manually adding Position and Heading.

To do that add Transform Component to it because that is the real identity of GameObject that is coming in the injected GameObjectArray. (It is actually ComponentArray IIRC)

And for continuously synchronizing game object data to entity you can put CopyTransformFromGameObjectSystem mono behaviour on it.

Thanks, works fine. Not sure why to have Position and to sync it to Transform via some special system or MonoBehaviour when I can just manipulate transform directly in system’s OnUpdate. Still interested in original question for structuring code, seems a good idea to describe set of components in archetypes even if it’s not pure entity. After all, I’m currently using ECS just for code structuring, I don’t really need big amounts of anything anywhere.

The bundled system that manipulate the Position based on Heading, Speed, etc. runs in thread so that’s one performance improvement. You can also create your own that exploit worker threads so it is better than OnUpdate. (Also Transform can be in thread by TransformAccessArray)

And if you decided to go pure ECS some day (draw things from Position/Rotation data rather than Transform data) you could potentially give those data directly to the drawer. But not at the moment as MeshInstanceRenderer still copy data out of ECS in order to render.

Can’t you do like typeof(Transform) typeof(RigidBody) in the archetype? I am not sure. But there’s also this way :

var go = new GameObject("test", typeof(Rigidbody));
GameObjectEntity.AddToEntityManager(m_Manager, go);
1 Like