protected override void OnUpdate() { var groupEntities = m_MainGroup.ToEntityArray(Allocator.TempJob); foreach (var entity in groupEntities) { var creator = EntityManager.GetComponentData <CreatePyramids>(entity); float3 boxSize = creator.BoxSize; int boxCount = creator.Count * (creator.Height * (creator.Height + 1) / 2); var positions = new NativeArray <float3>(boxCount, Allocator.Temp); int boxIdx = 0; for (int p = 0; p < creator.Count; p++) { for (int i = 0; i < creator.Height; i++) { int rowSize = creator.Height - i; float3 start = new float3(-rowSize * boxSize.x * 0.5f + boxSize.x * 0.5f, i * boxSize.y, 0); for (int j = 0; j < rowSize; j++) { float3 shift = new float3(j * boxSize.x, 0f, p * boxSize.z * creator.Space); positions[boxIdx] = creator.StartPosition; positions[boxIdx] += start + shift; boxIdx++; } } } var entities = new NativeArray <Entity>(boxCount, Allocator.Temp); EntityManager.Instantiate(creator.BoxEntity, entities); var pyramidComponent = new PhysicsPyramid(); for (boxIdx = 0; boxIdx < entities.Length; boxIdx++) { EntityManager.AddComponentData <PhysicsPyramid>(entities[boxIdx], pyramidComponent); EntityManager.SetComponentData <Translation>(entities[boxIdx], new Translation() { Value = positions[boxIdx] }); } entities.Dispose(); positions.Dispose(); PostUpdateCommands.DestroyEntity(entity); } groupEntities.Dispose(); }
protected override void OnUpdate() { Entities .WithoutBurst() .WithStructuralChanges() .ForEach((Entity creatorEntity, in CreatePyramids creator) => { float3 boxSize = creator.BoxSize; int boxCount = creator.Count * (creator.Height * (creator.Height + 1) / 2); var positions = new NativeArray <float3>(boxCount, Allocator.Temp); int boxIdx = 0; for (int p = 0; p < creator.Count; p++) { for (int i = 0; i < creator.Height; i++) { int rowSize = creator.Height - i; float3 start = new float3(-rowSize * boxSize.x * 0.5f + boxSize.x * 0.5f, i * boxSize.y, 0); for (int j = 0; j < rowSize; j++) { float3 shift = new float3(j * boxSize.x, 0f, p * boxSize.z * creator.Space); positions[boxIdx] = creator.StartPosition; positions[boxIdx] += start + shift; boxIdx++; } } } var pyramidComponent = new PhysicsPyramid(); for (int i = 0; i < positions.Length; i++) { var entity = EntityManager.Instantiate(creator.BoxEntity); EntityManager.AddComponentData(entity, pyramidComponent); EntityManager.SetComponentData(entity, new Translation() { Value = positions[i] }); } EntityManager.DestroyEntity(creatorEntity); }).Run(); }