1、创建,销毁Entity,以及对Entity的结构进行更改(比如增加删除Component)都要通过EntityManager等接口来实现。主线程实现Componet 数据操作修改
如下:
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
//都是实现了主线程对Componet 数据操作修改
state.EntityManager.Instantiate(solider_cfg.PrefabBlue, solider_entity_array);
state.EntityManager.SetComponentData(et, local_transform);
state.EntityManager.AddComponentData(et, wal_soldier);
...
}
2、如果你想将一个job中修改Component 修改,就必须使用ECB。
在Job中,只能实现对Entity的Component组件进行数据修改的功能。
在Job中需要通过Entity Command Buffer实现对Entity修改操作的记录;然后回到主线程进行统一操作。
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ecb_singleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
var job = new FJobUpdate ()
{
Ecb = ecb_singleton.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter(),
};
state.Dependency = job.ScheduleParallel(state.Dependency);
state.Dependency.Complete();
}
或者
public void OnUpdate(ref SystemState state)
{
EntityCommandBuffer ecbFinal = new EntityCommandBuffer(Allocator.Persistent, PlaybackPolicy.SinglePlayback);
var job = new FJobUpdate ()
{
Ecb = ecbFinal .AsParallelWriter(),
};
state.Dependency = job.ScheduleParallel(state.Dependency);
state.Dependency.Complete();
}
//传入到job中使用
public partial struct FJobUpdate : IJobEntity
{
public EntityCommandBuffer.ParallelWriter Ecb;
void Execute([ChunkIndexInQuery] int chunk_index){
var et_vfx = Ecb.Instantiate(chunk_index, skill_cfg.bufferPrefab);
Ecb.AddComponent(chunk_index, et_vfx, skill_vfx_tag);
Ecb.SetComponent(chunk_index, et_vfx, projectile_transform);
}
}