Hello, I am trying to instantiate prefabs. I have made the converter and the component where to save the entity that I want to instantiate. I have the problem in the System, I am using the EntityCommandBuffer ‘Instance’ method, it does not give me any error but nothing is being instantiated, the instances are not seen in the EntityDebugger. Any idea what is going wrong? The system code is below. Thank you.
public class SpawnerSystem : SystemBase
{
protected override void OnUpdate()
{
using (var ecb = new EntityCommandBuffer(Allocator.TempJob))
{
Entities.ForEach((in SpawnerData spawnerData) =>
{
ecb.Instantiate(spawnerData.PrefabEntity);
}).WithoutBurst().Run();
}
}
}
to elaborate, a command buffer system must be used to playback the buffer.
pseudocode
// in OnCreate, EndSimulationEntityCommandBufferSystem is just one of several builtin systems
commandBufferSystem = World.GetOrCreate<EndSimulationEntityCommandBufferSystem>();
// in OnUpdate
var ecb = commandBufferSystem.CreateCommandBuffer();
// your foreach job here, note you need to pass in the job's handle for the command buffer system
ecbSystem.AddJobHandleForProducer(Dependency);
Do note that if you are simply writing main thread code it is simplest to just do this:
public class SpawnerSystem : SystemBase
{
protected override void OnUpdate()
{
Entities.WithStructuralChange().ForEach((in SpawnerData spawnerData) =>
{
EntityManager.Instantiate(spawnerData.PrefabEntity);
}).Run();
}
}
EntityCommandBuffer is really only necessary when you want to write bursted & jobified code.
Ultimately thats necessary to get good performance, but if you are just writing prototype code the above is simpler.