Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Unity.Burst;
- using Unity.Collections;
- using Unity.Entities;
- using Unity.Jobs;
- using Unity.Mathematics;
- using static Unity.Mathematics.math;
- using Unity.Transforms;
- // JobComponentSystems can run on worker threads.
- // However, creating and removing Entities can only be done on the main thread to prevent race conditions.
- // The system uses an EntityCommandBuffer to defer tasks that can't be done inside the Job.
- public class SpawnCabotsSystem : JobComponentSystem
- {
- // EndSimulationBarrier is used to create a command buffer which will then be played back when that barrier system executes.
- EndSimulationEntityCommandBufferSystem m_EntityCommandBufferSystem;
- Random rng;
- protected override void OnCreateManager()
- {
- // Cache the EndSimulationBarrier in a field, so we don't have to create it every frame
- m_EntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
- rng = new Random(1);
- }
- struct SpawnJob : IJobForEachWithEntity<CabotsSpawner>
- {
- public EntityCommandBuffer CommandBuffer;
- public Random rng;
- public void Execute(Entity spawner_entity, int index, [ReadOnly] ref CabotsSpawner spawner)
- {
- for (int i = 0; i < spawner.Count; i++)
- {
- var ent = CommandBuffer.CreateEntity();
- var props = normalize(rng.NextFloat4()) * 2 - 1;
- CommandBuffer.AddComponent(
- ent,
- new CabotPropertiesComponent
- {
- Value = props
- }
- );
- CommandBuffer.AddBuffer<InventoryGoodBE>(ent);
- CommandBuffer.AddComponent(ent, new BasicNeeds { power = 0.1f });
- CommandBuffer.AddBuffer<TaxGoodBE>(ent);
- }
- CommandBuffer.DestroyEntity(spawner_entity);
- }
- }
- protected override JobHandle OnUpdate(JobHandle inputDeps)
- {
- //Instead of performing structural changes directly, a Job can add a command to an EntityCommandBuffer to perform such changes on the main thread after the Job has finished.
- //Command buffers allow you to perform any, potentially costly, calculations on a worker thread, while queuing up the actual insertions and deletions for later.
- // Schedule the job that will add Instantiate commands to the EntityCommandBuffer.
- var job = new SpawnJob
- {
- Comma
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement