Hi there!
I have been following several tutorials, and from what I have seen, a spawner system is usually used to spawn entities?
I would instead like to lean into the hybrid approach and manage the spawning from a simple MonoBehaviour
script.
To instantiate an entity at runtime, I use the World.DefaultGameObjectInjectionWorld.EntityManager
to CreateEntity()
, however, I would like to spawn from a GameObject prefab. The GameObjectConversionUtility
seems to be deprecated though, which used to be used for this purpose and has been replaced with the baking system. Is this still possible?
Thank you for your time!
You do have to bake your prefabs, which means you’ll need a subscene, but it’s still possible.
The public ECS sample contains ecs api examples, but it should be straightforward to pipe the relevant data to a MonoBehaviour
.
I have an object catalog that contains all the registered game object prefabs that can be created at runtime. Do I create an authoring script that bakes all the objects in the catalog into entities? There are many objects there, more than a hundred. Also, how do I get a specific baked entity and how do I reference it?
I am yet to grasp how ECS fully works and I am sorry if my questions seem stupid. Would appreciate the advice.
Check this then maybe you can use the instantiation in a MonoBehaviour.
For anyone who came to this thread like I did, the solution is much simpler and does’t require you to setup a whole spawner system for each Entity you want to spawn.
You can use EntityCommandBuffer
’s to setup an entity and all its components then use the commandBuffer.Playback(entityManager);
to run the buffer and create your entity. Look to these docs for more details Entity Command Buffer Doc.
Here is a quick example of I am using it inside a MonoBehvaiour, without spawner systems:
EntityCommandBuffer commandBuffer = new EntityCommandBuffer(Allocator.Temp);
Entity thing= commandBuffer.CreateEntity();
commandBuffer.AddComponent(thing, new LocalTransform { Position = new float3(buildingData.position.x, buildingData.position.y, 0) });
commandBuffer.Playback(World.DefaultGameObjectInjectionWorld.EntityManager);
Hope this help you all save some time searching through documentation.
1 Like